handleClick(i) { const squares = this.state.squares.slice(); squares[i] = 'X'; this.setState({squares: squares}); }
Determining if a mutated object has changed is complex because changes are made directly to the object. This then requires comparing the current object to a previous copy, traversing the entire object tree, and comparing each variable and value. This process can become increasingly complex.
Determining how an immutable object has changed is considerably easier. If the object being referenced is different from before, then the object has changed. That's it.
以上摘自React的引导, 有个关于slice()的疑问: 为什么要复制一份数据呢? 使用slice()复制出来的数据对比差异还是要全部遍历
ITMISS
相关分类