Last active
April 10, 2018 20:42
-
-
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)
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
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> | |
) | |
} |
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
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