Skip to content

Instantly share code, notes, and snippets.

@Lucbug
Last active April 10, 2018 20:42
Show Gist options
  • Save Lucbug/2a8751af045c219b374cd701aba4f277 to your computer and use it in GitHub Desktop.
Save Lucbug/2a8751af045c219b374cd701aba4f277 to your computer and use it in GitHub Desktop.
[GS] [React] Parent component (Interface.js) sets defaults state and gets updated by child's props (Settings.js)
constructor () {
this.state = {
// Set default values
settings : {
fps: 40,
bitrate: 8
}
};
}
onChangeSettings(newSettings){
// New settings shall be an object
this.setState({
settings: newSettings;
});
render() {
return (
<div>
<Settings changeSettings=this.onChangeSettings.bind(this) initialSettings={this.state.settings}>
</div>
)
}
constructor(props) {
this.state = {
//We want the same default settings as earlier when we get to the component
//If you pass the props in the constructor you don't have to use "this." before props
settings: props.intialSettings
};
}
_handleFpsChange(e) {
this.state.settings.fps = e.target.value;
}
onChangeSettings() {
this.props.changeSettings(this.state.settings);
}
render() {
return (
<div>
<input onChange={this._handleFpsChange.bind(event)} value={this.state.settings.fps}>
<button onClick={this.onChangeSettings.bind(this}>Change settings</button>
</div>
)
}
Settings.propTypes = {
initialSettings = React.PropTypes.object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment