ReactJS

ReactJS | Handling Events with Example

Hello React Friends,

I will explain event handling in ReactJS in detail in this tutorial guide.

Event handling allows users to interact with a web page and perform actions when events like click or hovering occur. Following are some events that are triggered by the system:

  • Clicking an element
  • Hovering an element
  • Scrolling page
  • Loading a webpage
  • Image loading
  • Submitting a form

Event Handling in ReactJS

React event handling is similar to event handling on DOM elements. There are some syntax differences in event handling for React and HTML.

Difference Between Html And React Event Handling

React events are written in camelcase instead of lowercase.

onClick instead of onclick.

React event handlers are passed as a function instead of a string. They are written inside curly braces.

onClick={fireme} instead of onClick=“fireme()”.

Event handling example in React:

<button onClick={fireme}>Click to fire me</button>

Event handling example in HTML:

<button >

Example:

import React from 'react';
import ReactDOM from 'react-dom/client';

function MyFireArea()
{
    const fireme = () =>
    {
        alert(“I am Burn!”);
    }
    return (
        <button onClick={fireme}>Click to fire me</button>
    );
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyFireArea />);

Passing Arguments to Event Handlers

Pass an argument to an event handler using an arrow function.

Example:

function MyFireArea()
{
    const fireme = (myArg) =>
    {
        alert(myArg);
    }
    return (
        <button onClick={() => fireme(“I am Burned”)}>Click to fire me</button>
    );
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyFireArea />);

React Event Object

Event handlers have access to the react event that triggered the function.

Example:

function MyFireArea()
{
    const fireme = (myArg, b) =>
    {
        alert(b.type);
        /*
        'b' represents the React event that triggered the function,
        in this case the 'click' event
        */    }
    return (
        <button onClick={(event) => fireme("I am Burned", event)}>Take the shot!</button>
    );
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyFireArea />);

Conclusion:

Event handling in React is the same as handling events in HTML. There are just some differences in syntax between the two. I hope the above example helped you to understand event handling in ReactJS.

Find more blogs on ReactJS – Click here!

Share the tutorial with your friends and stay in touch with us.

Happy Coding!

Click to rate this post!
[Total: 2 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

7 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago