Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amogower/ca248da3fa7254e239d1cd79cf700799 to your computer and use it in GitHub Desktop.
Save amogower/ca248da3fa7254e239d1cd79cf700799 to your computer and use it in GitHub Desktop.
Callback setState example
updateState({target}) {
this.setState(prevState => {
const updatedUser = {...prevState.user, [target.name]: target.value}; // use previous value in state to build new state...
doSomething(updatedUser); // Now I can safely utilize the new state I've created to call other funcs...
return { user: updatedUser }; // And what I return here will be set as the new state
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment