在render
实际更新状态之后,是否有任何方法可以确保调用setState
?据我所知,setState
是异步的。我曾尝试将render
作为对setState
的回调,但是在状态更新之前它仍会呈现。
HTML代码:
<div id="root"></div>
反应代码:
class Table extends React.Component {
constructor(props) {
super(props);
this.state = {
squares: []
};
this.create = this.create.bind(this);
}
create() {
console.log('squares before: ' + this.state.squares);
var a = [];
for (var i = 0; i < 16; i++) {
a.push(undefined);
}
i = Math.floor(Math.random() * 8);
a[i] = 2;
i = Math.floor(Math.random() * 8) + 8;
a[i] = 2;
console.log('new squares: ' + squares);
console.log('non-updated squares: ' + this.state.squares);
this.setState({
squares: a
}, console.log('updated squares: ' + this.state.squares);
}
componentWillMount() {
this.create();
}
render() {
console.log('rendering');
return (<div>
<table className="table">
<tbody>
<tr>
<td className="td"><Number number={this.state.squares[0]}/></td>
<td className="td"><Number number={this.state.squares[1]}/></td>
<td className="td"><Number number={this.state.squares[2]}/></td>
<td className="td"><Number number={this.state.squares[3]}/></td>
</tr>
<tr>
<td className="td"><Number number={this.state.squares[4]}/></td>
<td className="td"><Number number={this.state.squares[5]}/></td>
<td className="td"><Number number={this.state.squares[6]}/></td>
<td className="td"><Number number={this.state.squares[7]}/></td>
</tr>
<tr>
<td className="td"><Number number={this.state.squares[8]}/></td>
<td className="td"><Number number={this.state.squares[9]}/></td>
<td className="td"><Number number={this.state.squares[10]}/></td>
<td className="td"><Number number={this.state.squares[11]}/></td>
</tr>
<tr>
<td className="td"><Number number={this.state.squares[12]}/></td>
<td className="td"><Number number={this.state.squares[13]}/></td>
<td className="td"><Number number={this.state.squares[14]}/></td>
<td className="td"><Number number={this.state.squares[15]}/></td>
</tr>
</tbody>
</table>
</div>);
}
}
class Game extends React.Component {
newGame() {
this.refs.table.create();
}
render() {
return (<div>
<Table ref="table"/>
<button id="newGame" onClick={this.newGame.bind(this)}>New Game</button>
</div>);
}
}
基本上,这种情况发生的是先记录“渲染”,然后再记录“更新后的正方形:...”:
squares before: [undefined, 2, undefined, undefined, undefined, 2]
new squares: [undefined, undefined, 2, undefined, 2, undefined]
non-updated squares: [undefined, 2, undefined, undefined, undefined, 2]
rendering
updated squares: [undefined, undefined, 2, undefined, 2, undefined]
我尝试对setState
render
进行回调,但是第二个渲染将在第一个渲染之后但在setState
更新state
之前渲染:
this.setState({
squares: a
}, this.render);
它记录:
squares before: [undefined, 2, undefined, undefined, undefined, 2]
new squares: [undefined, undefined, 2, undefined, 2, undefined]
non-updated squares: [undefined, 2, undefined, undefined, undefined, 2]
rendering
rendering
任何帮助将不胜感激。预先感谢!
答案 0 :(得分:2)
synchronous
中的setState
componentWillMount
不会触发重新渲染,而是反映初始渲染本身的更改。但是,在render方法之后调用setState的回调。
如果将state squares
记录在render方法中,它将在初始渲染本身中记录正确的结果。
P.S。。componentWillMount
在最新版本中已过时,最好在构造函数中使用当前逻辑而不是componentWillMount
答案 1 :(得分:0)
使用shouldComponentUpdate(object nextProps,object nextState)进行渲染,每次返回true都会进行渲染