-
Notifications
You must be signed in to change notification settings - Fork 45.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
this.props and NextProps the same after parent state changes #7121
Comments
It's likely because you're mutating the current const settings = this.state.settings;
console.log('old settings: ', settings); // Produces expected (old state) results
settings[key] = !settings[key]; Since you're mutating the current toggleSetting = (key) => {
const settings = this.state.settings;
this.setState({
settings: !this.state.settings
});
}; React expects you to treat state as immutable, so doing it this way is always recommended. See
|
@aweary of course. I would normally do the snippet you just showed but I am trying to change a particular key within an object on state. As it turns out, I am doing this incorrectly in a few places. Had my console.log been 1 line lower, I would have caught that I was changing the original state this way. I often forget that. Anyway I've found the Immutability Helpers which work great. Thanks for the response. |
Thanks, you really helped me 😊😊 |
I am changing the parent state of a component from a nested child after passing a callback method down through props. This is working as expected, as the state is being changed and my components are updating properly. However, while optimizing my rendering I noticed that for this particular prop
settings
it ends up being the newest prop in bothcomponentWillReceiveProps
andshouldComponentUpdate
.So on my parent component I have a
setting
object like so:All behavior works as expected, except for the incoming props and "old" props being the same
The text was updated successfully, but these errors were encountered: