最佳答案
我是 React.js Library 的新手,在浏览一些教程时,我发现:
this.setStatethis.replaceState给出的描述不是很清楚(IMO)。
setState is done to 'set' the state of a value, even if its already set
in the 'getInitialState' function.
同样地,
The replaceState() method is for when you want to clear out the values
already in state, and add new ones.
我尝试了 this.setState({data: someArray});和 this.replaceState({test: someArray});,然后是 console. 日志,我发现 state现在同时拥有 data和 test。
然后,我尝试了 this.setState({data: someArray});和 this.setState({test: someArray});,然后 console. 记录了它们,我发现 state又同时拥有 data和 test。
那么,这两者之间到底有什么区别呢?