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

标签 javascript reactjs frontend

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

相关文章:

javascript - 动态构建 JSON-Data 和 JSONschema 的显示

javascript - 使用逗号在 jquery 中显示多个选定的跨度值

javascript - 如何从 div 类 Cypress 中获取文本

javascript - 如何在 Istanbul 尔覆盖 React jsx 文件?

javascript - 带有状态组件的reactjs + styled-components - 我的样式看不到 Prop

javascript - jquery 的 getscript() 函数在外部文件中调用时会在哪里查找?

css - Sharepoint 2013 任务列表中元素的样式

javascript - 在 javascript 中实现 if then else if else 堆栈的更简单方法是什么

php - 动态隐藏显示

javascript - Reducer 未捕获 LOCATION_CHANGE 操作