React / 78
Bubbling, stopPropagation & preventDefault
An event runs the clicked element's handler, then bubbles up through every parent's handler. stopPropagation stops the climb, and preventDefault stops the browser's own action.
Events bubble up
When you click an element, React first runs that element's handler, then its parent's, then the grandparent's, all the way up. That climb is called bubbling (or propagation).
export default function DishCard() {
return (
<div className="card" onClick={() => console.log("open dish page")}>
<h3>Masala dosa</h3>
<button onClick={() => console.log("add to cart")}>Add</button>
</div>
);
}Click Add and the console shows add to cart, then open dish page. Click anywhere else on the card and only open dish page runs.
Watch a click bubble
Try itdiv.card onClick
div.row onClick
- Handlers that ran will appear here, in order.
stopPropagation: stop the climb
Call it in the child's handler to keep the parents' handlers from running:
<button
onClick={(e) => {
e.stopPropagation(); // the card's onClick won't run
addToCart();
}}
>
Add
</button>It stops every handler above, including ones you didn't write, like an analytics wrapper or a dropdown that closes when you click outside it. So use it on purpose, not by habit. Often you can avoid it entirely: let the child call a handler prop (onAdd), so the whole chain of what runs is visible in the code.
preventDefault: stop the browser's own action
Some events come with a built-in browser behaviour, called the default action. e.preventDefault() cancels it. It has nothing to do with bubbling.
| Event | Default action |
|---|---|
Submitting a <form> | Sends the data by loading a new page (a full reload) |
Clicking a link <a href> | Navigates to the URL |
| Clicking a checkbox | Toggles it |
| Pressing Space on the page | Scrolls down |
The one you'll use constantly is the form submit:
export default function SearchForm() {
function handleSubmit(e) {
e.preventDefault(); // stay on this page
const query = new FormData(e.currentTarget).get("q");
console.log("Searching for", query);
}
return (
<form onSubmit={handleSubmit}>
<input name="q" aria-label="Search dishes" />
<button type="submit">Search</button>
</form>
);
}Without that line, the browser reloads the page and your app's state is gone. (React 19's <form action={fn}>, covered later in this chapter, calls it for you.)
Don't
preventDefault to stop the parent
<button onClick={(e) => {
e.preventDefault();
addToCart();
}}>Add</button>Outside a form, a button has no default action to cancel, so nothing changes. The card still opens.
Do
stopPropagation to stop the parent
<button onClick={(e) => {
e.stopPropagation();
addToCart();
}}>Add</button>Parents' handlers are what you wanted to stop, so stop the propagation.
The capture phase (rarely needed)
Bubbling is actually the last of three phases. Before it, the event travels down from the root, calling any handler whose name ends in Capture:
div.card
div.row
- 1capturediv.card onClickCapture
- 2capturediv.row onClickCapture
- 3targetbutton onClick
- 4bubblediv.row onClick
- 5bubblediv.card onClick
<div onClickCapture={() => logClick()}>
<button onClick={(e) => e.stopPropagation()}>Buy</button>
</div>logClick runs first, even though the button stops propagation, because capture already happened on the way down. That's useful for analytics or routers that must see every click. In everyday app code you'll almost never need it.
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
