Skip to content

Instantly share code, notes, and snippets.

@jide
Created July 12, 2016 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jide/50e9d764aedf99aa58ae12c22749bc25 to your computer and use it in GitHub Desktop.
Save jide/50e9d764aedf99aa58ae12c22749bc25 to your computer and use it in GitHub Desktop.
class Example extends Component {
componentWillReceiveProps(nextProps) {
// Only update state if needed.
if (nextProps.startTime !== this.state.startTime) {
// Change state using new prop value.
this.setState({
seconds: toSeconds(this.props.startTime)
})
}
}
handleChange = value => {
// The value entered by user is valid, let's dispatch some potentially
// heavy redux action. This will then set the state in
// componentWillReceiveProps().
if (isValid(value)) {
this.props.saveItem(value)
}
// Otherwise, just update state, this should be fast.
else {
this.setState({
seconds: toSeconds(value)
})
}
}
render() {
return <Input value={ this.state.seconds } onChange={ this.handleChange }/>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment