Event Handling in React JS

 Event Handling in React JS


React allows you to handle events in a similar way to HTML. Here's an example of how to handle a button click event:

jsx


class Button extends React.Component {

  handleClick() {

    console.log('Button clicked');

  }


  render() {

    return (

      <button onClick={() => this.handleClick()}>Click me</button>

    );

  }

}


ReactDOM.render(

  <Button />,

  document.getElementById('root')

);

In this example, the Button component has a method handleClick that logs a message to the console. The onClick prop of the button element is set to a function that calls the handleClick method.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...