Say a component's state is as follows:
{
a: { b: {c: 1, d: 1 }}
}
I want to change a.b.c
. I heard that in React we should immutable object to change state. So may be i should clone the object of state.a
, change the c
field and pass it to setState
:
const newA = JSON.parse(JSON.stringify(this.state.a)); // could use other clone method, just for example
newA.b.c = 5 // change
this.setState({ a: newA })
So what is the best way to do this? I am new to React. In Vue, we just change it and every thing happens. I don't know React's principle.