Created
April 20, 2019 18:44
-
-
Save iMega/829299e48ebf85df8e48b5e30cca0299 to your computer and use it in GitHub Desktop.
Using Class Components (>= [email protected])
This file contains 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 { | |
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