Created
July 12, 2016 22:57
-
-
Save jide/50e9d764aedf99aa58ae12c22749bc25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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