Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Last active April 1, 2018 12:40
Show Gist options
  • Save jordanrios94/d51e134f7b1ffe76dd1d7e0d6df06cf1 to your computer and use it in GitHub Desktop.
Save jordanrios94/d51e134f7b1ffe76dd1d7e0d6df06cf1 to your computer and use it in GitHub Desktop.
react component parent and child states (jsx is an example, this is just showing how to update parent state)
class ParentComponent extends Component{
state = {
parentValue: ''
}
handleParentValueChange = (value) => {
this.setState({parentValue: value});
}
render() {
return (
<div className="col-sm-9" >
<ChildComponent handleParentValueChange={this.handleParentValueChange}/>
</div>
)
}
}
export class ChildComponent extends Component {
state = {
childValue: ''
}
handleChildChange = () => {
var value = this.state.childValue;
this.props.handleParentValueChange(value);
}
render() {
return (
<div >
<input onChange={this.handleChildChange} />
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment