React Synthetic Events
Learn what Synthetic Events are in React, how to access event information, and how to handle user interactions with simple examples.
1. What Are Synthetic Events?
A Synthetic Event is React's event object that provides a consistent interface for handling browser events.
For example, when a user clicks a button, React passes an event object to the event handler. We can use this object to find the event type and the element that triggered it.
function SyntheticEvent() {
function handleClick(event) {
console.log("Event type:", event.type);
}
return (
<button onClick={handleClick}>
Click Me
</button>
);
}
export default SyntheticEvent;
Explanation:
When the button is clicked, React passes the event
object to handleClick(). The function
reads event.type.
2. How Does a Synthetic Event Work?
React receives the browser interaction and provides an event object to the event handler. The handler can then use the event information.
User Action
The user clicks a button.
React Event Object
React provides event information.
Event Handler
The function reads the event properties.
Result
The application responds to the interaction.
3. Important Event Properties
The event object contains useful properties. These help us understand what happened and which element was involved.
| Property | Simple Meaning |
|---|---|
event.type |
Returns the event type, such as click. |
event.target |
Identifies the element that triggered the event. |
event.currentTarget |
Identifies the element whose handler is running. |
event.key |
For keyboard events, identifies the key pressed. |
event.preventDefault() |
Prevents the browser's default action when applicable. |
event.stopPropagation() |
Stops the event from propagating further. |
event.key is useful for keyboard
events, not ordinary button clicks.
4. Using event.type
The event.type property tells us which event
occurred. For example, clicking a button produces the
event type click.
function EventType() {
function handleClick(event) {
console.log(event.type);
}
return (
<button onClick={handleClick}>
Check Event Type
</button>
);
}
export default EventType;
Explanation:
After clicking the button, the preview displays
click, which is the event type.
5. Using event.target
The event.target property identifies the element
that triggered the event.
function EventTarget() {
function handleClick(event) {
console.log("Clicked element:", event.target.tagName);
}
return (
<button onClick={handleClick}>
Inspect Element
</button>
);
}
export default EventTarget;
event.target.tagName gives the HTML tag name
of the element that triggered the event. For this button,
the result is BUTTON.
6. Using event.currentTarget
The event.currentTarget property identifies the
element whose event handler is currently executing.
function CurrentTarget() {
function handleClick(event) {
console.log(
"Handler element:",
event.currentTarget.tagName
);
}
return (
<button onClick={handleClick}>
Check Current Target
</button>
);
}
export default CurrentTarget;
event.currentTarget.tagName
returns BUTTON.
7. Difference Between target and currentTarget
These properties can identify different elements when an event occurs on a child element inside a parent element.
function TargetComparison() {
function handleClick(event) {
console.log("Target:", event.target.tagName);
console.log(
"Current target:",
event.currentTarget.tagName
);
}
return (
<div onClick={handleClick}>
Parent
<button type="button">
Click Child
</button>
</div>
);
}
export default TargetComparison;
event.target
The element where the event originated. If the child button is clicked, it is the button.
event.currentTarget
The element whose handler is running. Here, the handler is attached to the parent div.
Click either the parent area or the child button.
8. Synthetic Events and Native Events
Browsers generate native events. React provides a Synthetic Event wrapper so event handling follows a consistent interface across supported environments.
Native Browser Event
- Comes from the browser.
- Follows browser event APIs.
- Can be accessed through native event mechanisms.
React Synthetic Event
- Provided to React event handlers.
- Offers a consistent event interface.
- Provides access to the underlying event through
nativeEvent.
function NativeEvent() {
function handleClick(event) {
console.log("React event:", event.type);
console.log("Native event:", event.nativeEvent.type);
}
return (
<button onClick={handleClick}>
Check Events
</button>
);
}
export default NativeEvent;
event.nativeEvent
when you specifically need the native event.
9. Common Mistakes
1. Forgetting to receive the event parameter
function handleClick(event) {
console.log(event.type);
}
The event handler can receive the event object as its parameter when React invokes it.
2. Confusing target and currentTarget
console.log(event.target);
console.log(event.currentTarget);
The target is where the event originated; the current target is where the running handler is attached.
3. Assuming every event has the same properties
function handleKeyDown(event) {
console.log(event.key);
}
Properties such as key are useful for
keyboard events. Choose properties according to the
event type.
10. Practice Questions
- What is a Synthetic Event in React?
- Why does React provide a Synthetic Event object?
- What information does
event.typeprovide? - Explain the difference between
targetandcurrentTarget. - What is the purpose of
event.nativeEvent? - Write a React button that displays its event type.
- Create a parent and child example to compare target and currentTarget.
Summary
React Synthetic Events provide a consistent interface for handling
user interactions. The event object contains useful properties
such as type, target, and
currentTarget. React handlers can also access the
underlying browser event through nativeEvent.
Understanding these properties helps you write better event handlers.