Skip to content
ClapelloNotes by Barath
ReactCh 14 · 3/6
Core4 min readUpdated

React / 78

Bubbling, stopPropagation & preventDefault

TL;DR

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).

DishCard.jsx
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 it

div.card onClick

div.row onClick

  1. Handlers that ran will appear here, in order.
Click the button (or the empty part of a box). Then turn on stopPropagation: the parents go quiet. Turn on capture too: the card's capture handler still runs first, even though the button stopped the event.

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.

EventDefault 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 checkboxToggles it
Pressing Space on the pageScrolls down

The one you'll use constantly is the form submit:

SearchForm.jsx
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

button: you click here
  1. 1capturediv.card onClickCapture
  2. 2capturediv.row onClickCapture
  3. 3targetbutton onClick
  4. 4bubblediv.row onClick
  5. 5bubblediv.card onClick
One click, three phases. Capture handlers run on the way down, then the clicked element's onClick, then every parent's onClick on the way up. Almost all app code uses only the way up (bubbling).
<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.