Event Propagation in React
Learn how events travel between child and parent elements in React, with simple explanations, examples, and interactive output.
1. What Is Event Propagation?
Event propagation describes how an event travels through elements when a user interacts with a webpage.
For example, a button may be placed inside a div. If the user clicks the button, the button's click handler runs, and the event can also reach the parent div.
function EventPropagation() {
function handleParentClick() {
console.log("Parent clicked");
}
function handleChildClick() {
console.log("Child clicked");
}
return (
<div onClick={handleParentClick}>
Parent
<button onClick={handleChildClick}>
Child Button
</button>
</div>
);
}
export default EventPropagation;
Explanation: When the child button is clicked, its handler runs first. Then the click event bubbles up to the parent div, so the parent's handler also runs.
2. What Is Event Bubbling?
Event bubbling means an event starts at the element where the interaction occurred and then travels upward through its ancestors.
Child Button
The user clicks the button.
Parent DIV
The click event bubbles to the parent.
Parent Handler Runs
The parent's click handler executes.
Click the child button inside this area.
- Waiting for a click...
Child handler executed Parent handler executed
3. What Is stopPropagation()?
The stopPropagation() method prevents the
current event from continuing to propagate to other
ancestors.
We can use it when we want the child handler to run without triggering the parent's click handler through bubbling.
function StopPropagation() {
function handleParentClick() {
console.log("Parent clicked");
}
function handleChildClick(event) {
event.stopPropagation();
console.log("Child clicked");
}
return (
<div onClick={handleParentClick}>
Parent
<button onClick={handleChildClick}>
Child Button
</button>
</div>
);
}
export default StopPropagation;
event.stopPropagation() inside the child
handler to stop that event from bubbling to the parent.
It does not automatically prevent the browser's default
action; use preventDefault() for that purpose.
Click the child button to stop bubbling.
- Waiting for a click...
Child handler executed
The parent handler does not run for that child click.
4. Event Bubbling with Three Elements
Events can bubble through multiple ancestor elements. In this example, the button is inside a child div, which is inside a parent div.
function ThreeLevels() {
return (
<div onClick={() => console.log("Grandparent")}>
Grandparent
<div onClick={() => console.log("Parent")}>
Parent
<button
onClick={() => console.log("Child")}>
Click Me
</button>
</div>
</div>
);
}
export default ThreeLevels;
- Waiting for a click...
Child handler executed Parent handler executed Grandparent handler executed
5. Event Capturing vs Event Bubbling
Event propagation can also include a capturing phase. During capturing, the event travels from the outer ancestors toward the target. During bubbling, it travels back upward.
Capturing Phase
The event travels from the outer element toward the element that was clicked.
<div onClickCapture={handleParent}>
<button onClick={handleChild}>
Click Me
</button>
</div>
Bubbling Phase
The event travels from the clicked element back toward its ancestors.
<div onClick={handleParent}>
<button onClick={handleChild}>
Click Me
</button>
</div>
onClickCapture for a click
handler during the capture phase. A regular
onClick handler normally runs during bubbling.
- Waiting for a click...
Parent capture handler executed Child handler executed Parent bubble handler executed
6. Difference Between preventDefault() and stopPropagation()
These two methods have different purposes. One prevents a default browser action, while the other stops event propagation.
| Method | Purpose | Example |
|---|---|---|
preventDefault() |
Prevents the default browser action. | Prevent a form's normal submission. |
stopPropagation() |
Stops the event from propagating further. | Stop a child click from reaching a parent handler. |
preventDefault() does not stop bubbling, and
stopPropagation() does not prevent a default
browser action. Use the method that matches your requirement.
7. Common Mistakes
1. Expecting the parent handler not to run
// The event bubbles to the parent
function handleChildClick() {
console.log("Child clicked");
}
// Stop bubbling when required
function handleChildClick(event) {
event.stopPropagation();
console.log("Child clicked");
}
If you do not stop propagation, a parent click handler can also run when the child is clicked.
2. Confusing event methods
// Prevent a default browser action
event.preventDefault();
// Stop propagation
event.stopPropagation();
These methods solve different problems. They are not interchangeable.
3. Forgetting the event parameter
function handleChildClick(event) {
event.stopPropagation();
}
Receive the event object as a parameter when you need
to call methods such as stopPropagation().
8. Practice Questions
- What is event propagation?
- What is event bubbling?
- What does
stopPropagation()do? - What is the difference between capturing and bubbling?
- Explain the difference between
preventDefault()andstopPropagation(). - Create a parent div and child button with separate click handlers.
- Modify the child handler so the parent handler does not run.
- Create a three-level example that demonstrates event bubbling.
Summary
Event propagation describes how events travel through nested
elements. During bubbling, an event moves from the target toward
its ancestors. Use stopPropagation() when you need
to stop that propagation. The capturing phase travels toward
the target, while preventDefault() prevents a
browser's default action.