React Events Introduction
Learn how React handles user interactions such as button clicks, keyboard input, form changes, and submissions with practical examples.
1. What Are Events in React?
An event is an action performed by a user or triggered by the browser. Examples include clicking a button, typing text, submitting a form, and moving the mouse.
React event handlers allow components to respond to these actions by executing JavaScript functions.
Example: Welcome Button
function Welcome() {
function showMessage() {
alert("Welcome to CIIT Training Institute!");
}
return (
<button onClick={showMessage}>
Click Me
</button>
);
}
export default Welcome;
Clicking the button displays a welcome alert.
2. Why Are Events Important?
Events make React applications interactive. They allow users to perform actions and receive responses from the application.
Mouse Events
Handle button clicks and mouse interactions.
onClick
Keyboard Events
Respond when users press keyboard keys.
onKeyDown
Form Events
Handle input changes and form submissions.
onChange
Focus Events
Respond when an input gains or loses focus.
onFocus
3. How Do React Events Work?
When an event occurs, React calls the function associated with the corresponding event prop.
User Action
Click a button
Event Occurs
React handles the interaction
Function Executes
The event handler runs
Output Appears
The application responds
function EventFlow() {
function handleClick() {
console.log("Button clicked!");
}
return (
<button onClick={handleClick}>
Click Me
</button>
);
}
export default EventFlow;
onClick={handleClick} passes the function
reference. onClick={handleClick()} calls the
function immediately during rendering.
4. React Event Naming Convention
React uses camelCase event props. In JSX, event handlers are passed using curly braces.
| Event Prop | Purpose | Example |
|---|---|---|
onClick |
Handles clicks | Button click |
onChange |
Handles value changes | Typing in an input |
onSubmit |
Handles form submission | Submitting a form |
onKeyDown |
Handles key presses | Pressing Enter |
onMouseOver |
Handles mouse-over events | Moving over an element |
onFocus |
Handles focus | Selecting an input |
✓ Correct in React JSX
<button onClick={handleClick}>
Click Me
</button>
✗ Incorrect for React JSX
<button onclick={handleClick}>
Click Me
</button>
5. Passing a Function to an Event
Pass the function reference when the function should execute after the event occurs.
function FunctionHandler() {
function handleClick() {
alert("Button clicked successfully!");
}
return (
<button onClick={handleClick}>
Click Me
</button>
);
}
export default FunctionHandler;
A button appears. Clicking it displays:
onClick={handleClick} to pass a handler.
Use onClick={() => handleClick()} when you
need to execute additional code or pass arguments later.
6. Understanding the Event Object
React passes an event object to the handler. It contains information about the event and the element involved.
function EventObject() {
function handleClick(event) {
console.log("Event type:", event.type);
console.log("Clicked element:", event.target);
}
return (
<button onClick={handleClick}>
Inspect Event
</button>
);
}
export default EventObject;
| Property / Method | Description |
|---|---|
event.type |
Identifies the event type. |
event.target |
Identifies the element that triggered the event. |
event.currentTarget |
Identifies the element whose handler is running. |
event.preventDefault() |
Prevents the browser's default action. |
event.stopPropagation() |
Stops further event propagation. |
7. Practical Example — Click Counter
In this example, clicking the button increases the count.
The React code uses useState() to store the count.
import { useState } from "react";
function ClickCounter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(previousCount => previousCount + 1);
}
return (
<div>
<h3>Click Count: {count}</h3>
<button onClick={handleClick}>
Click Me
</button>
</div>
);
}
export default ClickCounter;
CIIT Click Counter
Click the button and observe the counter.
0. Each click increases
the count by one. The Reset button in this lesson preview
returns it to zero.
Note: The preview uses JavaScript to demonstrate the result. The code shown above is the React implementation.
8. Common Mistakes in React Events
1. Using lowercase event names
// Incorrect
<button onclick={handleClick}>Click</button>
// Correct
<button onClick={handleClick}>Click</button>
2. Calling the handler immediately
// Incorrect for ordinary click handling
<button onClick={handleClick()}>Click</button>
// Correct
<button onClick={handleClick}>Click</button>
3. Passing a string instead of a function
// Incorrect
<button onClick="handleClick">Click</button>
// Correct
<button onClick={handleClick}>Click</button>
handleClick, handleSubmit,
and handleChange.
9. Practice Questions
- What is an event in React?
- Why do React applications need event handlers?
- What is the purpose of the
onClickprop? - Why should we pass a function reference to an event handler?
- What is the React event object?
- Explain the purpose of
onChangeandonSubmit. - Write a component that displays an alert when a button is clicked.
- Write a component that increases a counter on each click.
Summary
React events make components interactive by connecting user actions to JavaScript functions. Event props use camelCase, and handlers are passed using JSX expressions. The event object provides useful information about an interaction. Understanding event handling is essential for building responsive React applications.