Skip to content

Instantly share code, notes, and snippets.

@SangeetAgarwal
Last active August 23, 2018 14:28
Show Gist options
  • Save SangeetAgarwal/671a75d19d06999b28728605f5741326 to your computer and use it in GitHub Desktop.
Save SangeetAgarwal/671a75d19d06999b28728605f5741326 to your computer and use it in GitHub Desktop.
avoid-deep-merge
updateState({target}) {
this.setState(prevState => {
return {
user: {
...prevState.user,
[target.name]: target.value,
address: {
// first lay out all the properties within the address object
...prevState.user.address,
// now, overwrite the properties that need to be overwritten...
// each of the below statements are optional depending upon what needs to be overwritten
// street: 'a new street'
// city: 'a new city'
}
}
}
}, () => doSomething())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment