Skip to content

Instantly share code, notes, and snippets.

@SASUKE40
Created June 14, 2018 07:52
Show Gist options
  • Save SASUKE40/b96e8a2a98d03d0b84abd5b2a46ea71e to your computer and use it in GitHub Desktop.
Save SASUKE40/b96e8a2a98d03d0b84abd5b2a46ea71e to your computer and use it in GitHub Desktop.
class EmailInput extends Component {
state = {
email: this.props.defaultEmail,
prevPropsUserID: this.props.userID
};
static getDerivedStateFromProps(props, state) {
// Any time the current user changes,
// Reset any parts of state that are tied to that user.
// In this simple example, that's just the email.
if (props.userID !== state.prevPropsUserID) {
return {
prevPropsUserID: props.userID,
email: props.defaultEmail
};
}
return null;
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment