gpt4 book ai didi

javascript - (React.js)为什么状态没有立即改变?

转载 作者:行者123 更新时间:2023-12-03 04:05:11 24 4
gpt4 key购买 nike

我通过在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com