Distinguish default prevention and propagation
Trace a click through nested elements
A click can reach handlers on ancestor elements. For this ordinary nested click, the outer capture handler runs before the button handler, then the outer bubble handler runs. The wrapper below only observes events; the native button is the interactive control. Use the developer console to inspect the predicted order.
export default function App() { return <main><h1>Trail event trace</h1> <div onClickCapture={() => console.log('panel capture')} onClick={() => console.log('panel bubble')}> <button type="button" onClick={event => { console.log('button: ' + event.currentTarget.tagName); }}>Inspect trail</button> </div> </main>;}Activating Inspect trail should log panel capture, button: BUTTON, then panel bubble. currentTarget denotes the element whose handler is executing; target denotes the original event target, which can be a descendant. This describes React DOM event behavior, not a promise that every event bubbles: for example, onScroll is an exception. React wraps native events and the nativeEvent property exposes the underlying event; their currentTarget values need not agree. Modern React DOM does not require the old persist() workaround.
Stop bubbling when it is intentional
export default function App() { return <main><h1>Trail event trace</h1> <div onClickCapture={() => console.log('panel capture')} onClick={() => console.log('panel bubble')}> <button type="button" onClick={event => { event.stopPropagation(); console.log('inspect only'); }}>Inspect trail</button> </div> </main>;}The expected order is panel capture, inspect only. The already executed capture handler is not undone. Use this method only when the ancestor action genuinely must not receive the event. Explicit callback props can make coordination easier to trace. Do not rely on propagation stopping to enforce authorization.
Cancel a default action without stopping propagation
export default function App() { return <main><h1>Trail information</h1> <div onClick={() => console.log('panel bubble')}> <a href="#trail-details" onClick={event => { event.preventDefault(); console.log('navigation prevented'); }}>Demonstrate canceled navigation</a> </div> <h2 id="trail-details">Trail details</h2> <p>Oak loop is a fictional local route.</p> </main>;}For ordinary activation, the link does not follow its fragment, but the outer bubble handler still logs. This is a diagnostic demonstration only; real links should normally navigate and actions should normally use buttons. stopPropagation would instead stop further propagation without itself canceling the browser action. Calling both is appropriate only if both effects are needed. Returning false from a React handler is not a substitute. Portal and native-listener interactions are later topics.
Practice
Predict and then trace an ordinary click through a wrapper and a native button. Add stopPropagation in the button and compare the log. Separately test a form whose submit handler prevents default and whose parent observes submit. Explain why the two methods are independent.