React JS - Click Event & Function

We can use Events in React JS same as JavaScript.

Example 1 : 

When user clicks on the button, a message will be displayed on alert box :

In above, we made a new functional component file ClickEvent.js, in which user clicks on the button, a message will be displayed on alert box. So, for this we used an event OnClick & function abc(). 

In React, to call a function using event, we use following syntax : 
<tag eventname={function}>.....</tag>

Example 2 : Using Arrow Function


In the above example, we have used Arrow Function which makes code very less and easy.

Github Links :

https://github.com/eramitsinha/React-Dummy/blob/main/src/ClickEvent.js

https://github.com/eramitsinha/React-Dummy/blob/main/src/ClickEventArrow.js



Comments