,

Event handler in React Components

by

By adding event handler functions inside react component, we can respond to events in React Js

function MyButton() {
  function fnBtnClick() {
    alert('You clicked here!');
  }
  return (
    <button onClick={fnBtnClick}>
      Click here
    </button>
  );
}

Note that onClick={fnBtnClick} has no parentheses at the end. Should not call the event handler function: we only need to pass it down. React Js will be taken care to call your event handler when we clicks the button.

Leave a Reply

Your email address will not be published. Required fields are marked *