我通过在 players
内放置一个对象来更改此处的状态。但似乎状态并没有立即改变?这是我在其他地方制造的功能还是错误?
console.log("Before(length):"+this.state.players.length);
players=players.filter(player => player.id !== id2);}
this.setState({players});
console.log("After(length):"+this.state.players.length);
结果是
Before(length):6
After(length):6
之后应该是 5?
最佳答案
Think of setState() as a request rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass.
https://facebook.github.io/react/docs/react-component.html#setstate
关于javascript - (React.js)为什么状态没有立即改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590137/