一段时间以来,我一直在努力解决这个问题,但一直没有找到正确的答案。
在下面的代码中:
this.setState(prevState => counter: prevState.counter + 1);
prevState
是对当前状态的引用吗?还是它的副本?变异是好还是应该避免变异?
最佳答案
从文档...
prevState is a reference to the previous state. It should not be directly mutated. Instead, changes should be represented by building a new object based on the input from prevState and props.
https://facebook.github.io/react/docs/react-component.html
所以为了回答你的问题,prevState 是最后一次突变发生之前的状态。
关于javascript - `this.setState` 中的 prevstate -- 副本还是引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45619297/