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 Toolkit

Learn how Redux Toolkit simplifies state management in React applications.

1. What Is Redux Toolkit?

Redux Toolkit (RTK) is the recommended way to write Redux applications. It provides utilities that reduce repetitive code and make Redux store configuration and state updates easier.

Why Do We Need Redux Toolkit?

Traditional Redux often requires separate action types, action creators, reducers, and additional configuration. Redux Toolkit simplifies this process with functions such as configureStore() and createSlice().

Traditional Redux vs Redux Toolkit

Traditional Redux

  • More repetitive code
  • Action types may be declared separately
  • Reducers require manual update logic
  • Store setup can require more configuration

Redux Toolkit

  • Less boilerplate code
  • Actions are generated automatically by createSlice()
  • Reducers are organized into slices
  • configureStore() simplifies store setup

2. Important Redux Toolkit Functions

1. configureStore()

configureStore() creates and configures the Redux store. It can combine reducers and includes useful development checks by default.

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

const store = configureStore({
    reducer: {}
});

export default store;

2. createSlice()

createSlice() creates a slice of Redux state. It accepts a slice name, initial state, and reducer functions. It also generates action creators automatically.

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

const counterSlice = createSlice({
    name: "counter",
    initialState: {
        value: 0
    },
    reducers: {
        increment: (state) => {
            state.value += 1;
        }
    }
});

export const { increment } = counterSlice.actions;

export default counterSlice.reducer;

3. Provider

Provider from React-Redux makes the configured store available to components in the React component tree.

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

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

export default App;

4. useSelector()

useSelector() reads a selected value from the Redux store.

import { useSelector } from "react-redux";

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

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

5. useDispatch()

useDispatch() provides the dispatch function so a component can dispatch action creators.

import { useDispatch } from "react-redux";
import { increment } from "./counterSlice";

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

    return (
        <button onClick={() => dispatch(increment())}>
            Increment
        </button>
    );
}

3. Understanding Slices

A slice represents one part of the application's Redux state, along with the reducer logic that updates it. For example, an institute application may have separate slices for students, courses, and authentication.

Example: Institute Application State

Redux Store
↓
Student Slice
Course Slice
Auth Slice

Each slice manages a specific area of the application's state.

Remember: A slice is not a separate Redux store. Multiple slices can be combined in one store through configureStore().

4. Create a Counter Slice

Let's create a counter slice with increment, decrement, and reset operations.

File: counterSlice.js

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;

Explanation

name Identifies the slice.
initialState Defines the initial counter value.
reducers Defines how the counter state changes.
actions Generated automatically from the reducer names.

5. Configure the Redux Store

After creating the counter slice, configure the Redux store and register the slice reducer.

File: store.js

import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counterSlice";

export const store = configureStore({
    reducer: {
        counter: counterReducer
    }
});

The key named counter determines where this slice's state appears in the store. Therefore, the counter value can be accessed through state.counter.value.

Store Structure

Redux Store
↓
state.counter
↓
state.counter.value = 0

6. Connect Redux to React

Wrap the application with Provider so its components can access the configured Redux store.

File: main.jsx

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { Provider } from "react-redux";
import { store } from "./store";
import App from "./App";

createRoot(document.getElementById("root")).render(
    <StrictMode>
        <Provider store={store}>
            <App />
        </Provider>
    </StrictMode>
);
This example uses the standard React entry-point setup. Ensure the project has React, React DOM, Redux Toolkit, and React-Redux installed.

7. Display and Update Counter State

Use useSelector() to read the counter value and useDispatch() to send increment, decrement, and reset actions.

File: Counter.jsx

import { useSelector, useDispatch } from "react-redux";

import {
    increment,
    decrement,
    reset
} from "./counterSlice";

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

    const dispatch = useDispatch();

    return (
        <div>
            <h2>Counter: {count}</h2>

            <button onClick={() => dispatch(increment())}>
                Increment
            </button>

            <button onClick={() => dispatch(decrement())}>
                Decrement
            </button>

            <button onClick={() => dispatch(reset())}>
                Reset
            </button>
        </div>
    );
}

export default Counter;

File: App.jsx

import Counter from "./Counter";

function App() {
    return (
        <div>
            <h1>Redux Toolkit Counter</h1>
            <Counter />
        </div>
    );
}

export default App;

Expected Output

Counter: 0

Increment | Decrement | Reset

Click Increment Counter: 1
Click Increment again Counter: 2
Click Decrement Counter: 1
Click Reset Counter: 0

8. Advantages of Redux Toolkit

Less Boilerplate

Reduces repetitive action and reducer setup.

Easy Configuration

configureStore() simplifies store creation.

Organized State

Slices keep related state logic together.

Predictable Updates

Reducers provide a structured approach to state updates.

9. Practice Tasks

Task 1: Create a Counter Slice

Create a counter slice with increment, decrement, and reset reducers.

Task 2: Configure a Store

Register the counter reducer using configureStore().

Task 3: Create a Student Slice

Create a student slice that stores a student's name and course name.

Task 4: Build a Counter Component

Use useSelector() and useDispatch() to display and update the counter value.

Summary

Redux Toolkit is the recommended approach for developing Redux applications. configureStore() creates the Redux store, while createSlice() groups related state and reducer logic and generates action creators. React-Redux's Provider makes the store available to components, useSelector() reads state, and useDispatch() sends actions. Together, these tools make shared state management more organized and reduce repetitive code in React applications. In the next lesson, we will explore Redux Store and Slices in greater detail.