ReactJS: Why shouldn't I mutate a nested state?

I read the ReactJS documentation on setState. In particular, this line:

NEVER mutate this.state directly, since calling setState () can subsequently replace the mutation you made. Treat this.state as immutable.

But in my code, I do this:

var x = this.state.x;
x.y.z.push("foo"); // z is a list
this.setState({x:x});

This code is working correctly and is being updated. But according to the documentation, I break the rules. What is the problem with this approach? Are there any performance issues? Race conditions Will the Facebook development team scold me? Ideally, I would like to avoid the helpers of immutability and the rest of the craziness and make things simple.

+4
source share
3 answers

, , this.setState, , , .

, OOP ( , JS ...) getter setter . this.state.prop1 = "xyz";

, , . . , , .

- https://facebook.imtqy.com/react/docs/component-specs.html#updating-shouldcomponentupdate

+3

, React, React. React setState , , DOM DOM DOM DOM. , , - , , , setState. , .

, , , , setState , React. , , . , , , , (.. ) setState, .

EDIT: , , , , setState, - , setState, , . setState , setState, , , , .

+4

, shouldComponentUpdate .

-1
source

All Articles