Avoid mutating state in the example code
According to react documentation it is advised to: NEVER mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable. https://facebook.github.io/react/docs/animation.html In this particular case it is probably doesn't matter since setState is called directly after mutation, but it does provide a bad example of state mutation. Another way of removing an item from an array can be `newItems = this.state.slice(0,i).concat(this.state.slice(i+1))` however the meaning can be less obvious to some.
Timur Carpeev committed
Nov 27, 2015 at 12:33 UTC
1e98c785417676f24c12340d5b23b9b780ef3d45
1 file changed
+1
-1
docs/docs/10.1-animation.md
+1
-1
@@ -29,7 +29,7 @@ var TodoList = React.createClass({
29
this.setState({items: newItems});
30
},
31
handleRemove: function(i) {
32
- var newItems = this.state.items;
32
+ var newItems = this.state.items.slice();
33
newItems.splice(i, 1);
34
this.setState({items: newItems});
35
},