Created
February 23, 2020 04:47
-
-
Save jasminegmp/b73f3b33782249e046f11ed5e612237a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React from 'react'; | |
class Parent extends React.Component{ | |
constructor(props){ | |
super(props); | |
this.state = { | |
data: null | |
} | |
} | |
handleCallback = (childData) =>{ | |
this.setState({data: childData}) | |
} | |
render(){ | |
const {data} = this.state; | |
return( | |
<div> | |
<Child parentCallback = {this.handleCallback}/> | |
{data} | |
</div> | |
) | |
} | |
} | |
class Child extends React.Component{ | |
onTrigger = (event) => { | |
this.props.parentCallback("Data from child"); | |
event.preventDefault(); | |
} | |
render(){ | |
return( | |
<div> | |
<form onSubmit = {this.onTrigger}> | |
<input type = "submit" value = "Submit"/> | |
</form> | |
</div> | |
) | |
} | |
} | |
export default Parent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment