Skip to content

Instantly share code, notes, and snippets.

@AlekseyDevksh
Created January 10, 2019 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlekseyDevksh/85e9c47bd4bc6e0f1bcfb9e989bbf400 to your computer and use it in GitHub Desktop.
Save AlekseyDevksh/85e9c47bd4bc6e0f1bcfb9e989bbf400 to your computer and use it in GitHub Desktop.
Call child method from parent
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