Skip to content

Instantly share code, notes, and snippets.

@jasminegmp
Created February 23, 2020 04:47
Show Gist options
  • Save jasminegmp/b73f3b33782249e046f11ed5e612237a to your computer and use it in GitHub Desktop.
Save jasminegmp/b73f3b33782249e046f11ed5e612237a to your computer and use it in GitHub Desktop.
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