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
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(): formonSubmit(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
falsefrom 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 onevent.targetorevent.currentTargetin the parent is cleaner.- Passive listeners. React registers
touchstart,touchmoveandwheelas passive, sopreventDefault()inonWheeloronTouchMovehas 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
scrollandblurcannot be cancelled;event.cancelabletells you. - Native listeners on
document. Because React attaches to the root container,stopPropagation()in a React handler does stopdocumentlisteners from seeing the event, but it cannot stop native listeners on elements inside the root that already ran.