Web browsers come with extensive pre-programmed, default behaviors for various HTML elements:
<a> link: Navigates the browser to the URL specified in the href attribute.<form>: Reloads the page and dispatches a synchronous HTTP request to the server.In modern Single-Page Applications (SPAs), developers frequently need to suppress these native browser actions to execute custom client-side logic instead.
e.preventDefault()React's SyntheticEvent provides the standard e.preventDefault() method to stop the browser's default action.
preventDefault() vs stopPropagation()A frequent point of confusion among engineers is the difference between preventing defaults and stopping propagation:
e.preventDefault(): Stops the browser's default native action associated with the event (e.g., preventing a form from submitting or a link from navigating). It does NOT stop the event from bubbling up the React component tree!e.stopPropagation(): Stops the event from bubbling up to parent DOM nodes. It does NOT prevent the browser's default action on the element!If you click the "Bookmark" link:
e.preventDefault() ensures the browser does not navigate away to /course/react.e.stopPropagation() ensures handleCardClick on the outer <div> is not triggered.false Does NOT Work in ReactIn old legacy HTML inline event handlers (<form onsubmit="return false;">) and jQuery, returning false from an event handler implicitly prevented default actions and stopped propagation.
React does not support returning false:
e.preventDefault() and e.stopPropagation()?false from a React event handler fail to stop default behavior?<a> tag has an onClick handler with e.preventDefault(), what happens when the user clicks the link?e.preventDefault() and e.stopPropagation() inside a button click handler nested inside a card?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Handling Click, Change, and Submit Events | Rendering Lists with the Array map Method |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.