Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Props and Attributes

React stopPropagation and preventDefault

How stopPropagation and preventDefault work in React event handlers: stop bubbling, cancel default actions, passive listeners and common mistakes.

Two methods on the SyntheticEvent control what happens after your handler runs, and they do different jobs.

event.preventDefault() cancels the browser's default action for the event: following a link, submitting a form, checking a box through a label. event.stopPropagation() stops the event from bubbling to handlers on parent elements. You can use either, both, or neither, and neither one implies the other.

JSX
function handleSubmit(event) {
  event.preventDefault();   // do not reload the page
}
function handleInnerClick(event) {
  event.stopPropagation();  // do not tell the parent
}

Try it

preventDefault on a link
stopPropagation in nested handlers

Syntax

JSX
event.preventDefault(): void
event.stopPropagation(): void
event.isDefaultPrevented(): boolean
event.isPropagationStopped(): boolean
Method Description
preventDefault() Cancels the default action, if the event is cancelable.
stopPropagation() Prevents the event from reaching React handlers on ancestor elements (and native listeners registered above the React root).
isDefaultPrevented() / isPropagationStopped() Report whether the call happened.

Usage notes

  • Common uses of preventDefault(): form onSubmit (stops the full page reload), link clicks handled by client-side routing, keyboard handlers that take over a key such as Space for scrolling.
  • Returning false from a handler does nothing in React, unlike inline HTML handlers.
  • stopPropagation() is a last resort. It hides the event from code you may not control (analytics, "click outside" listeners). Often a check on event.target or event.currentTarget in the parent is cleaner.
  • Passive listeners. React registers touchstart, touchmove and wheel as passive, so preventDefault() in onWheel or onTouchMove has no effect and the browser logs a warning. Add a native listener with { passive: false } in an effect if you must cancel those.
  • Capture handlers run first and can stop the event before the target sees it; see Capture phase handlers.
  • Not every event is cancelable. For example scroll and blur cannot be cancelled; event.cancelable tells you.
  • Native listeners on document. Because React attaches to the root container, stopPropagation() in a React handler does stop document listeners from seeing the event, but it cannot stop native listeners on elements inside the root that already ran.