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

Introduction to Redux

Learn how Redux helps React applications manage shared state in a predictable and organized way.

1. What Is Redux?

Redux is a state management library that helps applications manage shared data in a centralized store. Instead of passing the same data through many components, components can access the shared state through Redux.

Why Do We Need Redux?

In a large React application, many components may need the same data. Managing this data separately in different components can make the application difficult to maintain. Redux provides a structured way to manage this shared state.

Example

Imagine a training institute application. The Navbar displays the logged-in student's name, the Profile page displays the same student's details, and the Dashboard displays the student's course information. These components may need access to shared data.

Without Redux

App Component
↓
Parent Component
↓
Child Component
↓
Required Component

Data may need to pass through multiple components using props.

With Redux

Navbar
Profile
Dashboard
↕
Redux Store

Components can access the shared state from the central store through the appropriate Redux bindings.

2. What Is a Redux Store?

A Redux Store is the central place where the application's Redux state is maintained. It holds the data that needs to be shared across components.

Example Store State

{
    student: {
        name: "Samadhan",
        course: "React JS",
        isLoggedIn: true
    }
}

In this example, the store contains student information. Different components can read the relevant information without manually passing it through every intermediate component.

Remember: The store manages application state. Components should use Redux APIs to read that state and dispatch actions when changes are required.

3. Important Redux Concepts

1. Store

The store holds the Redux state for the application.

// Example state
{
    count: 0
}

2. Action

An action describes what happened or what change is requested. Actions commonly contain a type and may contain additional data.

{
    type: "counter/increment"
}

3. Reducer

A reducer is a function that receives the current state and an action, then calculates the next state.

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

        default:
            return state;
    }
}

4. Dispatch

Dispatch sends an action to the Redux store so the reducer can process it and calculate the next state.

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

4. How Redux Works

Redux follows a predictable, one-way data flow. An action describes a change, the reducer calculates the next state, and the store holds the updated state.

Redux Data Flow

React Component
↓ Dispatches an action
Redux Store
↓ Sends action to reducer
Reducer
↓ Calculates next state
Updated Store State
↓ Subscribed components update
Updated UI

Step-by-Step Explanation

  1. The user clicks a button in a React component.
  2. The component dispatches an action.
  3. The reducer processes the action and calculates the next state.
  4. The Redux store holds the new state.
  5. React components subscribed to the changed state can re-render.

5. Simple Counter Example

Consider a counter application. When the user clicks the Increment button, an action is dispatched. The reducer calculates the next counter value.

Counter Reducer

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

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

        default:
            return state;
    }
}

Dispatching Actions

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

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

Expected State Changes

Initial state 0
After increment 1
After another increment 2
After decrement 1

Note: This is a simplified reducer example. A complete Redux application also needs a configured store and the appropriate React-Redux integration.

6. Redux with React

Redux can be integrated with React using the React-Redux library. React-Redux provides tools that allow components to access the Redux store and dispatch actions.

Provider

The Provider makes the Redux store available to the React component tree.

import { Provider } from "react-redux";
import { store } from "./store";

function App() {
    return (
        <Provider store={store}>
            <StudentDashboard />
        </Provider>
    );
}

useSelector

The useSelector hook reads the required value from the Redux store.

import { useSelector } from "react-redux";

function Counter() {
    const count = useSelector(
        (state) => state.counter.value
    );

    return <h2>Count: {count}</h2>;
}

useDispatch

The useDispatch hook provides the dispatch function, which components use to send actions.

import { useDispatch } from "react-redux";

function IncrementButton() {
    const dispatch = useDispatch();

    return (
        <button
            onClick={() => dispatch({
                type: "counter/increment"
            })}
        >
            Increment
        </button>
    );
}
Important: The examples above illustrate individual concepts. The state.counter.value path assumes a store structure that contains a counter slice with a value property. Modern Redux applications generally use Redux Toolkit to simplify store configuration and reducer creation.

7. Advantages of Redux

Centralized State

Shared application data can be managed in one store.

Predictable Updates

Actions and reducers provide a structured update process.

Easy Debugging

State changes can be inspected with Redux DevTools.

Component Sharing

Multiple components can access the same application state.

Redux is especially useful when an application has complex shared state. Small applications may work perfectly well with React's built-in state and Context APIs, so Redux is not necessary for every project.

8. Practice Tasks

Task 1: Understand the Store

Create an example state object containing a student's name, course name, and login status.

Task 2: Create Actions

Write action objects for incrementing and decrementing a counter.

Task 3: Write a Reducer

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

Task 4: Explain Redux Flow

Draw a diagram showing how a component dispatches an action, how a reducer calculates the next state, and how the UI receives updated state.

Summary

Redux is a state management library that helps React applications manage shared data through a centralized store. Its core concepts include the store, actions, reducers, and dispatch. Redux follows a predictable one-way data flow in which actions describe changes, reducers calculate the next state, and subscribed components respond to relevant updates. React-Redux connects React components to the store, while Redux Toolkit simplifies modern Redux development. In the next lesson, we will explore Redux Core Concepts in more detail.