Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

Event Propagation in React

Learn how events travel between child and parent elements in React, with simple explanations, examples, and interactive output.

1. What Is Event Propagation?

Event propagation describes how an event travels through elements when a user interacts with a webpage.

For example, a button may be placed inside a div. If the user clicks the button, the button's click handler runs, and the event can also reach the parent div.

Simple Example:
EventPropagation.jsx JSX
function EventPropagation() {
    function handleParentClick() {
        console.log("Parent clicked");
    }

    function handleChildClick() {
        console.log("Child clicked");
    }

    return (
        <div onClick={handleParentClick}>
            Parent
            <button onClick={handleChildClick}>
                Child Button
            </button>
        </div>
    );
}

export default EventPropagation;

Explanation: When the child button is clicked, its handler runs first. Then the click event bubbles up to the parent div, so the parent's handler also runs.

2. What Is Event Bubbling?

Event bubbling means an event starts at the element where the interaction occurred and then travels upward through its ancestors.

Step 1
Child Button

The user clicks the button.

↓
Step 2
Parent DIV

The click event bubbles to the parent.

↓
Step 3
Parent Handler Runs

The parent's click handler executes.

Interactive Bubbling Example
Parent DIV

Click the child button inside this area.

Event Log
  1. Waiting for a click...
Expected Output after clicking the child:
Child handler executed
Parent handler executed

3. What Is stopPropagation()?

The stopPropagation() method prevents the current event from continuing to propagate to other ancestors.

We can use it when we want the child handler to run without triggering the parent's click handler through bubbling.

StopPropagation.jsx JSX
function StopPropagation() {
    function handleParentClick() {
        console.log("Parent clicked");
    }

    function handleChildClick(event) {
        event.stopPropagation();
        console.log("Child clicked");
    }

    return (
        <div onClick={handleParentClick}>
            Parent
            <button onClick={handleChildClick}>
                Child Button
            </button>
        </div>
    );
}

export default StopPropagation;
Important: Call event.stopPropagation() inside the child handler to stop that event from bubbling to the parent. It does not automatically prevent the browser's default action; use preventDefault() for that purpose.
Interactive stopPropagation Example
Parent DIV

Click the child button to stop bubbling.

Event Log
  1. Waiting for a click...
Expected Output after clicking the child:
Child handler executed

The parent handler does not run for that child click.

4. Event Bubbling with Three Elements

Events can bubble through multiple ancestor elements. In this example, the button is inside a child div, which is inside a parent div.

ThreeLevels.jsx JSX
function ThreeLevels() {
    return (
        <div onClick={() => console.log("Grandparent")}>
            Grandparent
            <div onClick={() => console.log("Parent")}>
                Parent
                <button
                    onClick={() => console.log("Child")}>
                    Click Me
                </button>
            </div>
        </div>
    );
}

export default ThreeLevels;
Three-Level Bubbling Demo
Grandparent
Parent
Event Log
  1. Waiting for a click...
Expected Output after clicking the child:
Child handler executed
Parent handler executed
Grandparent handler executed

5. Event Capturing vs Event Bubbling

Event propagation can also include a capturing phase. During capturing, the event travels from the outer ancestors toward the target. During bubbling, it travels back upward.

Capturing Phase

The event travels from the outer element toward the element that was clicked.

<div onClickCapture={handleParent}>
    <button onClick={handleChild}>
        Click Me
    </button>
</div>

Bubbling Phase

The event travels from the clicked element back toward its ancestors.

<div onClick={handleParent}>
    <button onClick={handleChild}>
        Click Me
    </button>
</div>
Remember: In React, use onClickCapture for a click handler during the capture phase. A regular onClick handler normally runs during bubbling.
Interactive Capture and Bubble Demo
Parent (capture and bubble handlers)
Event Log
  1. Waiting for a click...
Expected Output after clicking the child:
Parent capture handler executed
Child handler executed
Parent bubble handler executed

6. Difference Between preventDefault() and stopPropagation()

These two methods have different purposes. One prevents a default browser action, while the other stops event propagation.

Method Purpose Example
preventDefault() Prevents the default browser action. Prevent a form's normal submission.
stopPropagation() Stops the event from propagating further. Stop a child click from reaching a parent handler.
Remember: preventDefault() does not stop bubbling, and stopPropagation() does not prevent a default browser action. Use the method that matches your requirement.

7. Common Mistakes

1. Expecting the parent handler not to run

// The event bubbles to the parent
function handleChildClick() {
    console.log("Child clicked");
}

// Stop bubbling when required
function handleChildClick(event) {
    event.stopPropagation();
    console.log("Child clicked");
}

If you do not stop propagation, a parent click handler can also run when the child is clicked.

2. Confusing event methods

// Prevent a default browser action
event.preventDefault();

// Stop propagation
event.stopPropagation();

These methods solve different problems. They are not interchangeable.

3. Forgetting the event parameter

function handleChildClick(event) {
    event.stopPropagation();
}

Receive the event object as a parameter when you need to call methods such as stopPropagation().

8. Practice Questions

  1. What is event propagation?
  2. What is event bubbling?
  3. What does stopPropagation() do?
  4. What is the difference between capturing and bubbling?
  5. Explain the difference between preventDefault() and stopPropagation().
  6. Create a parent div and child button with separate click handlers.
  7. Modify the child handler so the parent handler does not run.
  8. Create a three-level example that demonstrates event bubbling.

Summary

Event propagation describes how events travel through nested elements. During bubbling, an event moves from the target toward its ancestors. Use stopPropagation() when you need to stop that propagation. The capturing phase travels toward the target, while preventDefault() prevents a browser's default action.