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

Redux Core Concepts

Learn how the Redux Store, Actions, Reducers, and Dispatch work together to manage application state.

1. Introduction to Redux Core Concepts

Redux manages shared application state through a centralized store and a predictable update process. To understand Redux, we need to learn four important concepts: Store, Actions, Reducers, and Dispatch.

Example: Student Management Application

Imagine a student dashboard where the application maintains the total number of enrolled students. When a new student is added, the state must be updated and the displayed count should reflect the change.

Redux Core Concepts Overview

Store — Holds the State
↑ Updated State
Reducer — Calculates Next State
↑ Receives Action
Dispatch — Sends the Action
↑
Action — Describes What Happened

Dispatching an action causes the reducer to calculate the next state, which the store then holds.

2. Redux Store

The Redux Store is the central object that holds the application's Redux state. It provides methods for reading the current state, dispatching actions, and subscribing to state updates.

Example State

{
    students: {
        total: 10
    }
}

In this example, the state contains a students object with a total property. The application can use this value to display the current number of students.

Remember: A Redux store is not simply a normal JavaScript object. It manages the state and provides the Redux API for updating and observing that state.

3. Actions

An action is a plain JavaScript object that describes an event or a requested state change. A standard Redux action has a type property. It can also contain additional information through properties such as payload.

Example 1: Increment Action

{
    type: "counter/increment"
}

Example 2: Add Student Action

{
    type: "students/addStudent",
    payload: {
        name: "Samadhan",
        course: "React JS"
    }
}

Understanding the Action

type Identifies the event or operation.
payload Contains additional data needed for the operation.
An action describes what happened. It does not directly update the Redux store by itself. The action must be dispatched and processed by the reducer.

4. Reducers

A reducer is a function that receives the current state and an action, then returns the next state. Reducers should be predictable and must not directly mutate existing state.

Example: Counter Reducer

function counterReducer(state = 0, action) {
    switch (action.type) {
        case "counter/increment":
            return state + 1;

        case "counter/decrement":
            return state - 1;

        case "counter/reset":
            return 0;

        default:
            return state;
    }
}

How the Reducer Works

Current State 5
Action counter/increment
Next State 6

If the reducer receives the increment action while the current state is 5, it returns 6. If it receives an unknown action, the default case returns the current state unchanged.

Important: A reducer should not make API calls, update the DOM, or perform other side effects. It should calculate the next state from its inputs.

5. Dispatch

Dispatch is the mechanism used to send an action to the Redux store. The store processes the action through its reducer logic and holds the resulting state.

Example

dispatch({
    type: "counter/increment"
});

Dispatching Different Actions

dispatch({
    type: "counter/increment"
});

dispatch({
    type: "counter/decrement"
});

dispatch({
    type: "counter/reset"
});

Each dispatch sends an action describing the requested operation. The reducer decides how the state should change.

6. Complete Redux Flow

Let's understand how all four concepts work together using a simple counter example.

Step-by-Step Data Flow

1. User Clicks Increment
↓
2. Component Dispatches Action
↓
3. Reducer Processes Action
↓
4. Store Holds Next State
↓
5. React UI Reflects New State

Example State Changes

Initial state 0
After increment 1
After increment again 2
After decrement 1
After reset 0

7. Redux Toolkit Approach

Redux Toolkit is the recommended approach for writing Redux applications today. It reduces boilerplate and provides convenient functions for configuring stores and creating reducers.

Example: Creating a Counter Slice

import { createSlice } from "@reduxjs/toolkit";

const counterSlice = createSlice({
    name: "counter",

    initialState: {
        value: 0
    },

    reducers: {
        increment: (state) => {
            state.value += 1;
        },

        decrement: (state) => {
            state.value -= 1;
        },

        reset: (state) => {
            state.value = 0;
        }
    }
});

export const {
    increment,
    decrement,
    reset
} = counterSlice.actions;

export default counterSlice.reducer;
Redux Toolkit uses Immer internally for reducers created with createSlice. This allows the reducer code to use mutation-like syntax while producing immutable state updates. The same syntax should not be copied into an ordinary Redux reducer unless the update is handled appropriately.

In the next lesson, we will explore Redux Toolkit in more detail, including how to configure the store and use slices.

8. Practice Tasks

Task 1: Create Actions

Write action objects for adding a student, removing a student, and resetting a student counter.

Task 2: Write a Reducer

Create a counter reducer that handles increment, decrement, and reset actions.

Task 3: Predict State Changes

If the initial counter value is 3, determine the final value after increment, increment, decrement, and reset actions.

Task 4: Explain Redux Flow

Draw a diagram showing the relationship between the Store, Actions, Reducers, and Dispatch.

Summary

Redux manages shared application state through a centralized store and a predictable update process. The store holds the state, actions describe events or requested changes, reducers calculate the next state, and dispatch sends actions to the store. Together, these concepts help React applications maintain organized and predictable state updates. Redux Toolkit simplifies Redux development through utilities such as createSlice and configureStore. In the next lesson, we will learn how to use Redux Toolkit to build Redux applications more efficiently.