Skip to content

Instantly share code, notes, and snippets.

@iMega
Created April 20, 2019 18:44
Show Gist options
  • Save iMega/829299e48ebf85df8e48b5e30cca0299 to your computer and use it in GitHub Desktop.
Save iMega/829299e48ebf85df8e48b5e30cca0299 to your computer and use it in GitHub Desktop.
Using Class Components (>= [email protected])
class Parent extends Component {
constructor(props) {
super(props);
this.child = React.createRef();
}
onClick = () => {
this.child.current.getAlert();
};
render() {
return (
<div>
<Child ref={this.child} />
<button onClick={this.onClick}>Click</button>
</div>
);
}
}
class Child extends Component {
getAlert() {
alert('getAlert from Child');
}
render() {
return <h1>Hello</h1>;
}
}
ReactDOM.render(<Parent />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment