Articles → REACT.JS → Raise An Event From Parent Component And Handle The Event From Child Component In React.Js
Raise An Event From Parent Component And Handle The Event From Child Component In React.Js
Example
Click to Enlarge
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
class ChildComponent extends React.Component {
render() {
return (
<div><button onClick={this.props.onClick}>Click Me</button></div>
);
}
}
class ParentComponent extends React.Component {
handleClick = () =>{
alert("Button clicked");
};
render() {
return (
<ChildComponent onClick={this.handleClick}></ChildComponent>
);
}
}
ReactDOM.render(<ParentComponent />, document.getElementById('root'));
Click to Enlarge