-
-
Save eAmin/429b0e3a3b6cccd33d458ae2972a0ed3 to your computer and use it in GitHub Desktop.
Basic example to pass values between parent and child components in React.
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
/** | |
* Basic example to pass values between parent and child components in React | |
* Seems to be in line with this | |
* http://stackoverflow.com/questions/24147331/react-the-right-way-to-pass-form-element-state-to-sibling-parent-elements | |
* Now I have the state in parent and child. Is that good or bad? Why would I need it in child? | |
* Could probably take that out | |
* */ | |
class Parent extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
fieldVal: "" | |
} | |
} | |
onUpdate = (val) => { | |
this.setState({ | |
fieldVal: val | |
}) | |
}; | |
render() { | |
return ( | |
<div> | |
<h2>Parent</h2> | |
Value in Parent Component State: {this.state.fieldVal} | |
<br/> | |
<Child onUpdate={this.onUpdate} /> | |
<br /> | |
<OtherChild passedVal={this.state.fieldVal} /> | |
</div> | |
) | |
} | |
} | |
class Child extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
fieldVal: "" | |
} | |
} | |
update = (e) => { | |
console.log(e.target.value); | |
this.props.onUpdate(e.target.value); | |
this.setState({fieldVal: e.target.value}); | |
}; | |
render() { | |
return ( | |
<div> | |
<h4>Child</h4> | |
<input | |
type="text" | |
placeholder="type here" | |
onChange={this.update} | |
value={this.state.fieldVal} | |
/> | |
</div> | |
) | |
} | |
} | |
class OtherChild extends React.Component { | |
render() { | |
return ( | |
<div> | |
<h4>OtherChild</h4> | |
Value in OtherChild Props: {this.props.passedVal} | |
</div> | |
) | |
} | |
} | |
React.render( | |
<Parent />, | |
document.getElementById('react_example') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment