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

React Synthetic Events

Learn what Synthetic Events are in React, how to access event information, and how to handle user interactions with simple examples.

1. What Are Synthetic Events?

A Synthetic Event is React's event object that provides a consistent interface for handling browser events.

For example, when a user clicks a button, React passes an event object to the event handler. We can use this object to find the event type and the element that triggered it.

Simple Example:
SyntheticEvent.jsx JSX
function SyntheticEvent() {
    function handleClick(event) {
        console.log("Event type:", event.type);
    }

    return (
        <button onClick={handleClick}>
            Click Me
        </button>
    );
}

export default SyntheticEvent;

Explanation: When the button is clicked, React passes the event object to handleClick(). The function reads event.type.

Expected Console Output
> Event type: click

2. How Does a Synthetic Event Work?

React receives the browser interaction and provides an event object to the event handler. The handler can then use the event information.

Step 1
User Action

The user clicks a button.

↓
Step 2
React Event Object

React provides event information.

↓
Step 3
Event Handler

The function reads the event properties.

↓
Step 4
Result

The application responds to the interaction.

Remember: You do not need to create a Synthetic Event manually. React supplies the event object when the handler runs.

3. Important Event Properties

The event object contains useful properties. These help us understand what happened and which element was involved.

Property Simple Meaning
event.type Returns the event type, such as click.
event.target Identifies the element that triggered the event.
event.currentTarget Identifies the element whose handler is running.
event.key For keyboard events, identifies the key pressed.
event.preventDefault() Prevents the browser's default action when applicable.
event.stopPropagation() Stops the event from propagating further.
Note: Some properties are specific to particular event types. For example, event.key is useful for keyboard events, not ordinary button clicks.

4. Using event.type

The event.type property tells us which event occurred. For example, clicking a button produces the event type click.

EventType.jsx JSX
function EventType() {
    function handleClick(event) {
        console.log(event.type);
    }

    return (
        <button onClick={handleClick}>
            Check Event Type
        </button>
    );
}

export default EventType;
Interactive Output Preview
Console result: Waiting for a click...

Explanation: After clicking the button, the preview displays click, which is the event type.

5. Using event.target

The event.target property identifies the element that triggered the event.

EventTarget.jsx JSX
function EventTarget() {
    function handleClick(event) {
        console.log("Clicked element:", event.target.tagName);
    }

    return (
        <button onClick={handleClick}>
            Inspect Element
        </button>
    );
}

export default EventTarget;
Simple Explanation: event.target.tagName gives the HTML tag name of the element that triggered the event. For this button, the result is BUTTON.
Interactive Output Preview
Clicked element: Not clicked yet

6. Using event.currentTarget

The event.currentTarget property identifies the element whose event handler is currently executing.

CurrentTarget.jsx JSX
function CurrentTarget() {
    function handleClick(event) {
        console.log(
            "Handler element:",
            event.currentTarget.tagName
        );
    }

    return (
        <button onClick={handleClick}>
            Check Current Target
        </button>
    );
}

export default CurrentTarget;
Simple Explanation: In this example, the handler is attached to the button. Therefore, event.currentTarget.tagName returns BUTTON.
Interactive Output Preview
Handler element: Not clicked yet

7. Difference Between target and currentTarget

These properties can identify different elements when an event occurs on a child element inside a parent element.

TargetComparison.jsx JSX
function TargetComparison() {
    function handleClick(event) {
        console.log("Target:", event.target.tagName);
        console.log(
            "Current target:",
            event.currentTarget.tagName
        );
    }

    return (
        <div onClick={handleClick}>
            Parent
            <button type="button">
                Click Child
            </button>
        </div>
    );
}

export default TargetComparison;

event.target

The element where the event originated. If the child button is clicked, it is the button.

event.currentTarget

The element whose handler is running. Here, the handler is attached to the parent div.

Interactive Parent and Child Example
Parent DIV

Click either the parent area or the child button.

Target: —
Current target: —

8. Synthetic Events and Native Events

Browsers generate native events. React provides a Synthetic Event wrapper so event handling follows a consistent interface across supported environments.

Native Browser Event

  • Comes from the browser.
  • Follows browser event APIs.
  • Can be accessed through native event mechanisms.

React Synthetic Event

  • Provided to React event handlers.
  • Offers a consistent event interface.
  • Provides access to the underlying event through nativeEvent.
NativeEvent.jsx JSX
function NativeEvent() {
    function handleClick(event) {
        console.log("React event:", event.type);
        console.log("Native event:", event.nativeEvent.type);
    }

    return (
        <button onClick={handleClick}>
            Check Events
        </button>
    );
}

export default NativeEvent;
Expected Console Output
> React event: click
> Native event: click
Remember: React's Synthetic Event is not the same object as the underlying browser event. Use event.nativeEvent when you specifically need the native event.

9. Common Mistakes

1. Forgetting to receive the event parameter

function handleClick(event) {
    console.log(event.type);
}

The event handler can receive the event object as its parameter when React invokes it.

2. Confusing target and currentTarget

console.log(event.target);
console.log(event.currentTarget);

The target is where the event originated; the current target is where the running handler is attached.

3. Assuming every event has the same properties

function handleKeyDown(event) {
    console.log(event.key);
}

Properties such as key are useful for keyboard events. Choose properties according to the event type.

10. Practice Questions

  1. What is a Synthetic Event in React?
  2. Why does React provide a Synthetic Event object?
  3. What information does event.type provide?
  4. Explain the difference between target and currentTarget.
  5. What is the purpose of event.nativeEvent?
  6. Write a React button that displays its event type.
  7. Create a parent and child example to compare target and currentTarget.

Summary

React Synthetic Events provide a consistent interface for handling user interactions. The event object contains useful properties such as type, target, and currentTarget. React handlers can also access the underlying browser event through nativeEvent. Understanding these properties helps you write better event handlers.