[英]How to use React to setState and avoid recursion?
I'm using React with a Flask server, and have a table where a user can enter information. 我正在使用React与Flask服务器,并有一个用户可以输入信息的表。 This child table updates the tableData
state of the MainApp when the user edits a cell, adds a new row, deletes a row etc. 此子表更新tableData
的MainApp的状态下,当用户编辑一个单元格,添加一个新行,删除一行等等。
My problem is that I'm using an image
state in the MainApp to store the image URL (this URL contains the hash as well so it changes every time a client requests for information). 我的问题是我在MainApp中使用image
状态来存储图像URL(此URL也包含哈希值,因此每次客户端请求信息时它都会更改)。 However, I'm also using the tableData
state in the MainApp to store the table information which changes when a user updates the table. 但是,我还使用了tableData
中MainApp状态存储在用户更新表,更改表信息。 This calls componentWillUpdate
which in turn gets the image blob, converts it to a URL and updates the state image
. 这会调用componentWillUpdate
,后者又获取图像blob,将其转换为URL并更新状态image
。 This in turn calls componentWillUpdate
as you might imagine leading to a recursive call. 这反过来会调用componentWillUpdate
因为您可能会想到导致递归调用。 The React documentation says to not call setState
inside componentWillUpdate
. React文档说不要在componentWillUpdate
调用setState
。 I'm curious as to what the recommended way of doing this is. 我很好奇推荐的做法是什么。
My intended behaviour is that the user edits a cell on the table, MainApp sends a request to the server with the table information, MainApp receives an image and updates the page accordingly. 我的预期行为是用户编辑表格上的单元格,MainApp使用表格信息向服务器发送请求,MainApp接收图像并相应地更新页面。 Should I not use image
state to store the URL? 我不应该使用image
状态来存储URL吗?
One way to do this is to use setState
in componentWillUpdate
and use shouldComponentUpdate
with logic based on a globalVariable ( shouldComponentUpdate
would be called multiple times) but I'm pretty sure that is probably not the recommended approach. 一种方法是在componentWillUpdate
使用setState
,并使用带有基于shouldComponentUpdate
的逻辑的shouldComponentUpdate( shouldComponentUpdate
将被多次调用),但我很确定这可能不是推荐的方法。
var globalVariable = true
var MainApp = React.createClass({
shouldComponentUpdate : function () {
// logic here using globalVariable
}
componentWillUpdate : function () {
console.log("Get image from server")
fetch('/api/data', {
method: 'POST',
body: JSON.stringify({
tableData: this.state.tableData,
})
})
.then(function(response) {
return response.blob();
}.bind(this))
.then(function(imageBlob) {
this.setState({
image: URL.createObjectURL(imageBlob),
tableData: this.state.tableData
})
}.bind(this))
},
handleTableUpdate : function(newState) {
this.setState({
image: this.state.image,
tableData: newState
});
console.log("Table updated")
},
render : function() {
return (
<div>
<div id="inverter-table" className="col-sm-6">
<MyReactBootstrapTable
onTableUpdate={this.handleTableUpdate}
data={this.state.tableData} />
</div>
<div id="img" className="col-sm-6">
<MyPlot url={this.state.image}></MyPlot>
</div>
</div>
);
}
})
You can identify the changed property in componentWillReceiveProps and then call setState accordingly. 您可以在componentWillReceiveProps中标识已更改的属性,然后相应地调用setState。 This will not create recursion in the code. 这不会在代码中创建递归。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.