-
ReactJS Events
-
User interaction is the foundation of any modern web application. Clicking buttons, typing text, submitting forms, or moving the mouse are all actions that users perform constantly. In React, these interactions are handled using React Events.
Understanding React events is critical because they connect user actions, state changes, and UI updates. Whether you are building a simple form or a complex dashboard, events play a central role.
What Are Events in React?
Events in React represent user actions performed on UI elements, such as:
- Clicking a button
- Typing into an input
- Submitting a form
- Hovering over elements
- Pressing keyboard keys
React allows components to respond to these actions by attaching event handlers to elements.
How React Events Work?
React uses a system called Synthetic Events. Instead of working directly with browser events, React creates a synthetic event that wraps the original event.
This approach:- Ensures consistent behavior across browsers
- Provides a unified event API
- Improves performance through internal optimizations
Event Naming Convention in React
React uses camelCase for event names.
HTML Event React Event Description onclick onClick Triggers when user clicks on an element (button, div, link). onchange onChange Triggers when user changes the value of an input, select, or textarea. onsubmit onSubmit Executes when a form is submitted. onmouseover onMouseOver Triggered when the mouse pointer moves over an element. onkeydown onKeyDown Fires when a key on the keyboard is pressed down.
Lets understand how each one of these works in react with examples.
1. onClick – Handling Click Events
A click event is triggered when a user interacts with an element—such as pressing a button or selecting an image—and the browser executes a corresponding action.
In React, handling this interaction is straightforward by attaching a function to the onClick attribute of the element.
Functional Component Example
Class Component Examplefunction OnClickExample() { function handleClick() { alert("Button clicked"); } return ( <button onClick={handleClick}>Click Me</button> ); }
Common use casesclass OnClickExample extends React.Component { handleClick() { alert("Button clicked"); } render() { return ( <button onClick={() => this.handleClick()}>Click Me</button> ); } }- Submitting actions
- Opening modals
- Incrementing counters
2. onChange – Tracking Input Changes
The
onChangeevent is fired whenever the value of an input element—such as a text box, textarea, or dropdown—gets updated by the user. This could happen while typing, selecting an option, or modifying existing input.In React,
Functional Component ExampleonChangeis commonly used to capture user input in real time and keep the component state in sync with form fields. It plays a key role in building controlled components, where the UI always reflects the latest data entered by the user.
Class Component Examplefunction OnChangeExample() { const [value, setValue] = React.useState(""); function handleChange(event) { setValue(event.target.value); } return ( <> <input type="text" onChange={handleChange} /> <p>{value}</p> </> ); }
Common use casesclass OnChangeExample extends React.Component { state = { value: "" }; handleChange(event) { this.setState({ value: event.target.value }); } render() { return ( <> <input type="text" onChange={(e) => this.handleChange(e)} /> <p>{this.state.value}</p> </> ); } }- Controlled forms
- Search boxes
- Live input validation
3. onSubmit – Handling Form Submission
The
In React,onSubmitevent is triggered when a user submits a form, typically by clicking a submit button or pressing the Enter key inside an input field. This event signals that the user has finished entering data and is ready to send it for processing.onSubmitis used to manage how form data is handled before it is sent to a server or used within the application. Functional Component Example
Class Component Examplefunction OnSubmitFormExample() { function handleSubmit(event) { event.preventDefault(); console.log("Form submitted"); } return ( <form onSubmit={handleSubmit}> <button type="submit">Submit</button> </form> ); }
Common Use Cases:class OnSubmitFormExample extends React.Component { handleSubmit(event) { event.preventDefault(); console.log("Form submitted"); } render() { return ( <form onSubmit={(e) => this.handleSubmit(e)}> <button type="submit">Submit</button> </form> ); } }- Login and registration forms
- Contact forms
- Preventing page reload during form submission
4. onMouseOver – Mouse Enter Event
The
onMouseOverevent occurs when the user moves the mouse pointer onto an element or one of its child elements. It helps detect when an element gains focus due to a mouse interaction.In React,
Functional Component ExampleonMouseOveris commonly used to enhance user experience by adding interactive behaviors such as highlighting buttons, showing tooltips, displaying preview information, or triggering animations. Since it responds immediately when the cursor enters the element area, it is useful for providing visual feedback and making interfaces feel more responsive and engaging.
Class Component Examplefunction OnMouseOverExample() { function handleMouseOver() { console.log("Mouse entered"); } return ( <div onMouseOver={handleMouseOver}>Hover over me</div> ); }
Common Use Cases:class OnMouseOverExample extends React.Component { handleMouseOver() { console.log("Mouse entered"); } render() { return ( <div onMouseOver={() => this.handleMouseOver()}>Hover over me</div> ); } }- Showing tooltips or hints
- Highlighting UI elements
- Revealing additional information on hover
5. onMouseOut – Mouse Leave Event
The
Functional Component ExampleonMouseOutevent is fired when the mouse pointer moves away from an element or exits its boundary, including its child elements. It indicates that the user is no longer interacting with that part of the interface.onMouseOutis often paired withonMouseOverto reverse or reset visual changes.
Class Component Examplefunction MouseOutExample() { function handleMouseOut() { console.log("Mouse left"); } return ( <div onMouseOut={handleMouseOut}>Move mouse away</div> ); }
Common Use Cases:class MouseOutExample extends React.Component { handleMouseOut() { console.log("Mouse left"); } render() { return ( <div onMouseOut={() => this.handleMouseOut()}>Move mouse away</div> ); } }- Hiding tooltips
- Resetting hover styles
- Closing hover-based menus
- Removing highlight effects
6. onKeyDown – Detecting Keyboard Input
The
onKeyDownevent is triggered the moment a user presses a key on the keyboard, before the key is released. It allows applications to react instantly to keyboard actions.
Functional Component ExampleonKeyDowntracks which key is pressed and allows you to respond based on that key.
Class Component Exampleimport React from "react"; function KeyDownExample() { const handleKeyDown = (event) => { if (event.key === "Enter") { alert("Enter key pressed!"); } else if (event.key === "Escape") { alert("Escape key pressed!"); } else { console.log(`Key pressed: ${event.key}`); } }; return ( <div> <h3>Keyboard Event Example</h3> <input type="text" placeholder="Press Enter or Escape" onKeyDown={handleKeyDown} /> </div> ); } export default KeyDownExample;
Common Use Cases:import React, { Component } from "react"; class KeyDownExample extends Component { handleKeyDown(event) { if (event.key === "Enter") { alert("Enter key pressed!"); } else if (event.key === "Escape") { alert("Escape key pressed!"); } else { console.log("Key pressed:", event.key); } } render() { return ( <div> <h3>Keyboard Event – Class Component</h3> <input type="text" placeholder="Press Enter or Escape" onKeyDown={(e) => this.handleKeyDown(e)} /> </div> ); } } export default KeyDownExample;- Detecting Enter or Escape key
- Accessibility improvements
- Custom input behavior
- Form submission via keyboard
React supports almost all standard browser events. The key difference is how these events are written.
List of events
HTML Event React Event Category Description onclick onClick Mouse Triggered when an element is clicked. ondblclick onDoubleClick Mouse Fires when an element is double-clicked. onmousedown onMouseDown Mouse Occurs when a mouse button is pressed. onmouseup onMouseUp Mouse Occurs when a mouse button is released. onmouseover onMouseOver Mouse Triggered when the pointer enters an element. onmouseout onMouseOut Mouse Triggered when the pointer leaves an element. onmousemove onMouseMove Mouse Fires when the mouse moves within an element. onmouseenter onMouseEnter Mouse Runs when the mouse enters an element (no bubbling). onmouseleave onMouseLeave Mouse Runs when the mouse leaves an element (no bubbling). onkeydown onKeyDown Keyboard Triggered when a key is pressed down. onkeyup onKeyUp Keyboard Triggered when a pressed key is released. onkeypress onKeyPress Keyboard Triggered when a character key is pressed (deprecated). onchange onChange Form Runs when input value changes. onsubmit onSubmit Form Triggered when a form is submitted. oninput onInput Form Fires immediately as the user types. onreset onReset Form Triggered when a form is reset. onfocus onFocus Focus Runs when an element gains focus. onblur onBlur Focus Runs when an element loses focus. onload onLoad UI Triggered when a resource finishes loading. onerror onError UI Runs when an error occurs during loading. oncopy onCopy Clipboard Triggered when content is copied. oncut onCut Clipboard Triggered when content is cut. onpaste onPaste Clipboard Triggered when content is pasted. ontouchstart onTouchStart Touch Triggered when a touch point starts. ontouchend onTouchEnd Touch Triggered when a touch point ends. ontouchmove onTouchMove Touch Triggered when a touch point moves. ontouchcancel onTouchCancel Touch Triggered when a touch is interrupted. ondrag onDrag Drag Fires while an element is being dragged. ondragstart onDragStart Drag Triggered when dragging starts. ondragend onDragEnd Drag Triggered when dragging ends. ondragover onDragOver Drag Runs when a dragged item is over a target. ondrop onDrop Drag Triggered when a dragged item is dropped.
Summary
React events provide a structured and predictable way to handle user interactions across applications. Whether using functional components or class components, event handling follows the same principles.
Events act as the bridge between user behavior, state updates, and UI changes. Mastering React events makes it much easier to work with forms, conditional rendering, and routing in real-world projects.