Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
Created May 30, 2018 09:54
Show Gist options
  • Save stephanbogner/fdb23a65bca8ac65d763047e8d0aec1a to your computer and use it in GitHub Desktop.
Save stephanbogner/fdb23a65bca8ac65d763047e8d0aec1a to your computer and use it in GitHub Desktop.
Most simple example to pass data from child to parent in react
class Parent extends React.Component{
useDataFromChild = (value) => {
console.log(value)
}
render() {
return (
<div>
<Child name="Option: Little" onReceiveData={this.useDataFromChild}/>
<Child name="Option: Many" onReceiveData={this.useDataFromChild}/>
</div>
)
}
}
class Child extends React.Component {
sendData = () => {
this.props.onReceiveData(this.props);
}
render() {
return (
<div >
<div onClick={this.sendData}>{this.props.name}</div>
</div>
);
}
}
ReactDOM.render(<Parent/>, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment