Created
May 30, 2018 09:54
-
-
Save stephanbogner/fdb23a65bca8ac65d763047e8d0aec1a to your computer and use it in GitHub Desktop.
Most simple example to pass data from child to parent 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
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