I've used componentDidUpdate()
in the past and it has worked as expected.
This time, however, I did
componentDidUpdate(prevProps, prevState) {
if (prevState.object.someString !== this.state.object.someString) {
console.log(true);
}
}
and true
is never logged. I logged both state objects to the console and found out they are exactly the same one: the current state.
Is this a bug? What am I missing here?
Thank you.
Edit: I tried to do the same thing with componentWillUpdate(nextProps, nextState)
and again, they're the same object.
Edit 2: I'm changing the state by doing:
modifyObject = (field, value) => {
const { object } = this.state;
object[field] = value;
this.setState({ object });
}
modifyObject = (field, value) => { const { object } = this.state; character[field] = value; this.setState({ object }); }
character
and where did it come from? You are not mutating yourobject
variable either. By the looks of it, you are just updating the state as it was previously