I am developing a React App and stumbled upon this behavior of the React state which i am not really sure if it's correct!?
I initiate my State like this:
componentDidMount() {
someRequest()
.then(...) //do stuff to get the Payload of the Response
.then(response => {
this.setState({
obj1: response,
obj2: response.someArr
})
})
}
And i have a formHandler that changes the state of obj2
formHandler = event => {
...
this.setState({obj2: newArrState})
}
I always assumed that the React state is immutable so that obj2 hold a new reference to a new object/array after the setState()
call.
However if i take a look at this.state.obj1
i also see the changes propagated to this.state.obj1.someArr
so that this.state.obj1.someArr === this.state.obj2
Is that a correct behavior?