Created
January 10, 2019 11:45
-
-
Save AlekseyDevksh/85e9c47bd4bc6e0f1bcfb9e989bbf400 to your computer and use it in GitHub Desktop.
Call child method from parent
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
class Parent extends Component { | |
render() { | |
return ( | |
<div> | |
<Child setClick={click => this.clickChild = click}/> | |
<button onClick={() => this.clickChild()}>Click</button> | |
</div> | |
); | |
} | |
} | |
class Child extends Component { | |
constructor(props) { | |
super(props); | |
this.getAlert = this.getAlert.bind(this); | |
} | |
componentDidMount() { | |
this.props.setClick(this.getAlert); | |
} | |
getAlert() { | |
alert('clicked'); | |
} | |
render() { | |
return ( | |
<h1 ref="hello">Hello</h1> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment