Last active
April 1, 2018 12:40
-
-
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)
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 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