How to Put an Event Handler in a Component Class


Fri Jun 17 2022 05:55:23 GMT+0000 (UTC)

Saved by @mehran1801 #react.js

you have to define an event handler before you can pass one anywhere. 

How do you define an event handler in React?

You define an event handler as a method on the component class, just like the render method.

class Example extends React.Component {
  handleEvent() {
    alert(`I am an event handler.
      If you see this message,
      then I have been called.`);

  render() {
    return (
      <h1 onClick={this.handleEvent}>
        Hello world