Event Handling

Events in HTML are also used in React. The only difference is their naming is different in React. Events in React are written in camelcase. Say – onclick becomes onClick.

  • Consider the following code in HTML:
<button onclick="activate()"> activate </button>

Translates to:

<button onClick={activate}> activate </button>

In class-based components, we can directly add arrow functions in the callback. If you are using class-based components then you have to bind the method with this otherwise you won’t be able to use this in the function.

  • Read more about event handling here.