Articles → REACT.JS → Event Handling In React.Js
Event Handling In React.Js
Example
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
class Message extends Component {
render() {
return (
<button onClick={this.TestMessage}>Click Me</button>
);
}
TestMessage() {
alert("test");
}
}
ReactDOM.render(<Message />, document.getElementById('root'));
Click to Enlarge
Passing Parameter In The Event Handler
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
import ChildComponent from './gyan.js';
class ParentComponent extends React.Component {
clickEvent = inputParam => {
alert(inputParam);
};
render() {
return (
<div><button onClick={() => this.clickEvent("karan") }>Click Me</button></div>
);
}
}
ReactDOM.render(<ParentComponent />, document.getElementById('root'));
Output
Click to Enlarge