MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Preventing Default Browser Behaviors0%
Study Materials
React.js for BeginnersProgress 0%
1.1Why React and Single-Page Architecture1.2Setting Up Modern React with Vite1.3React Project Directory Structure and Entry Points1.4Virtual DOM and React Render Cycle Explained
5.1Handling Synthetic Events in React5.2Passing Arguments to Event Handlers5.3Handling Click, Change, and Submit Events5.4Preventing Default Browser Behaviors
HomeTutorialsReact.js for BeginnersPreventing Default Browser Behaviors
Rendering Lists with the Array map Method

Preventing Default Browser Behaviors

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. What Are Default Browser Behaviors?

Web browsers come with extensive pre-programmed, default behaviors for various HTML elements:

  • Clicking an <a> link: Navigates the browser to the URL specified in the href attribute.
  • Submitting a <form>: Reloads the page and dispatches a synchronous HTTP request to the server.
  • Right-clicking an element: Opens the browser's native contextual menu.
  • Pressing the Spacebar or PageDown: Scrolls the viewport down.
  • Dragging an image: Displays a ghost silhouette of the dragged image.

In modern Single-Page Applications (SPAs), developers frequently need to suppress these native browser actions to execute custom client-side logic instead.

2. Using e.preventDefault()

React's SyntheticEvent provides the standard e.preventDefault() method to stop the browser's default action.

1import React from 'react';
2 
3export default function CustomLink({ url, children }) {
4 const handleClick = (e) => {
5 // Suppress browser from navigating and refreshing the page
6 e.preventDefault();
7 
8 console.log(`Internal navigation initiated to: ${url}`);
9 // Custom client-side router navigation (e.g., React Router navigate(url))
10 };
11 
12 return (
13 <a href={url} onClick={handleClick} className="spa-link">
14 {children}
15 </a>
16 );
17}

3. 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!
1function NestedCard() {
2 const handleCardClick = () => {
3 console.log('Card container clicked!');
4 };
5 
6 const handleFavoriteClick = (e) => {
7 // 1. Prevent parent card's onClick from triggering
8 e.stopPropagation();
9 
10 // 2. Prevent link from navigating away
11 e.preventDefault();
12 
13 console.log('Item saved to favorites!');
14 };
15 
16 return (
17 <div className="card" onClick={handleCardClick}>
18 <h3>Full-Stack React Course</h3>
19
20 <a href="/course/react" onClick={handleFavoriteClick} className="fav-btn">
21 ★ Bookmark
22 </a>
23 </div>
24 );
25}

If you click the "Bookmark" link:

  1. 1
    e.preventDefault() ensures the browser does not navigate away to /course/react.
  2. 2
    e.stopPropagation() ensures handleCardClick on the outer <div> is not triggered.

4. Why Returning false Does NOT Work in React

In 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:

1// ❌ FAILS in React: Returning false will NOT prevent page reload!
2<form onSubmit={() => false}>
3 <button type="submit">Submit</button>
4</form>
5 
6// ✅ CORRECT: Must explicitly invoke e.preventDefault()
7<form onSubmit={(e) => { e.preventDefault(); }}>
8 <button type="submit">Submit</button>
9</form>

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What method must be invoked to cancel the browser's default behavior for an event in React?
Question 2
What is the key difference between e.preventDefault() and e.stopPropagation()?
Question 3
Why does returning false from a React event handler fail to stop default behavior?
Question 4
If an <a> tag has an onClick handler with e.preventDefault(), what happens when the user clicks the link?
Question 5
What happens if you call both e.preventDefault() and e.stopPropagation() inside a button click handler nested inside a card?
Next Lesson

Rendering Lists with the Array map Method

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Handling Click, Change, and Submit EventsRendering Lists with the Array map Method

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext