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 Store and Slices

Learn how to configure a Redux store, create slices, combine reducers, and organize application state.

1. Introduction to Redux Store and Slices

In Redux Toolkit, the store holds the application's Redux state. A slice groups a particular part of that state with the reducer logic and action creators used to update it.

For example, a training institute application may manage student information, course information, and authentication information. Each area can have its own slice, and all these slices can be registered in a single Redux store.

Key Difference

The store manages the overall Redux state, while a slice manages one logical part of that state. Multiple slices can work together inside one store.

Redux Store Structure

Redux Store
↓
Student Slice
Course Slice
Auth Slice

Each slice manages its own section of the application's state.

2. What Is a Redux Store?

A Redux store is the central place that holds the Redux state. It provides a predictable way for application components to access shared data and request state changes through actions.

Creating a Store with configureStore()

Redux Toolkit provides configureStore() to create the store and register the reducer functions used by the application.

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

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

export default store;
Remember: The empty reducer object is only a starting example. A useful application registers one or more reducers in this object.

3. What Is a Slice?

A slice is a collection of Redux reducer logic and actions for one particular feature of an application. Redux Toolkit's createSlice() function generates action creators and a reducer from the slice definition.

Example: 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;

Understanding the Properties

name Identifies the slice.
initialState Defines the initial state for the slice.
reducers Contains functions that describe state updates.
actions Provides action creators generated from the reducers.
reducer Exports the reducer that can be registered in the store.
Redux Toolkit uses Immer internally with createSlice(). This allows reducer functions to use mutation-like syntax while producing immutable state updates.

4. Register a Slice in the Store

After creating a slice, import its reducer and register it inside configureStore(). The key used in the reducer object determines where that slice's state appears in the store.

File: store.js

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

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

Understanding the Store State

{
    counter: {
        value: 0
    }
}

Because the reducer is registered under the counter key, the state can be selected using state.counter.value.

State Access Path

Redux Store
↓
state.counter
↓
state.counter.value

5. Working with Multiple Slices

An application can have multiple slices. For example, a training institute application can have separate slices for students and courses.

File: studentSlice.js

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

const studentSlice = createSlice({
    name: "students",

    initialState: {
        total: 0
    },

    reducers: {
        addStudent: (state) => {
            state.total += 1;
        },

        resetStudents: (state) => {
            state.total = 0;
        }
    }
});

export const {
    addStudent,
    resetStudents
} = studentSlice.actions;

export default studentSlice.reducer;

File: courseSlice.js

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

const courseSlice = createSlice({
    name: "courses",

    initialState: {
        total: 0
    },

    reducers: {
        addCourse: (state) => {
            state.total += 1;
        },

        resetCourses: (state) => {
            state.total = 0;
        }
    }
});

export const {
    addCourse,
    resetCourses
} = courseSlice.actions;

export default courseSlice.reducer;

Register Both Slices

File: store.js

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

import studentReducer from "./studentSlice";
import courseReducer from "./courseSlice";

export const store = configureStore({
    reducer: {
        students: studentReducer,
        courses: courseReducer
    }
});

Resulting Store Structure

{
    students: {
        total: 0
    },

    courses: {
        total: 0
    }
}
The students and courses keys come from the keys configured in store.js. The slice names do not automatically determine these top-level state keys.

6. Read State Using useSelector()

The useSelector() hook reads the required data from the Redux store. A component can select state from any registered slice.

Example: Student Count

import { useSelector } from "react-redux";

function StudentCount() {
    const totalStudents = useSelector(
        (state) => state.students.total
    );

    return (
        <h2>
            Total Students: {totalStudents}
        </h2>
    );
}

export default StudentCount;

Example: Course Count

import { useSelector } from "react-redux";

function CourseCount() {
    const totalCourses = useSelector(
        (state) => state.courses.total
    );

    return (
        <h2>
            Total Courses: {totalCourses}
        </h2>
    );
}

export default CourseCount;

Both components access the same Redux store, but each selects data from a different slice.

7. Update State Using useDispatch()

useDispatch() provides the dispatch function. You can use it to dispatch action creators generated by createSlice().

Example: Add a Student

import { useDispatch } from "react-redux";
import { addStudent } from "./studentSlice";

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

    return (
        <button onClick={() => dispatch(addStudent())}>
            Add Student
        </button>
    );
}

export default AddStudentButton;

Example: Add a Course

import { useDispatch } from "react-redux";
import { addCourse } from "./courseSlice";

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

    return (
        <button onClick={() => dispatch(addCourse())}>
            Add Course
        </button>
    );
}

export default AddCourseButton;

Expected State Changes

Initial student count 0
After adding a student 1
Initial course count 0
After adding a course 1

8. Connect the Store with React

Use Provider from React-Redux to make the configured store available throughout the React component tree.

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>
);

File: App.jsx

import StudentCount from "./StudentCount";
import AddStudentButton from "./AddStudentButton";

import CourseCount from "./CourseCount";
import AddCourseButton from "./AddCourseButton";

function App() {
    return (
        <div>
            <h1>CIIT Student Dashboard</h1>

            <StudentCount />
            <AddStudentButton />

            <CourseCount />
            <AddCourseButton />
        </div>
    );
}

export default App;
Ensure all file paths match your project structure and that React, React DOM, Redux Toolkit, and React-Redux are installed. The examples in this lesson are intended to be combined into the corresponding files.

9. Best Practices for Store and Slices

Keep Related Logic Together

Group the initial state, reducers, and actions for a feature in the same slice.

Use Meaningful Names

Use clear slice names and reducer keys such as students, courses, and auth.

Keep Reducers Predictable

Avoid API requests and other side effects inside reducers. Use Redux Toolkit's async utilities for asynchronous work.

Select Only Required State

Use useSelector() to read the data a component actually needs.

10. Practice Tasks

Task 1: Create a Student Slice

Create a slice with a total property and reducers to add and reset the student count.

Task 2: Create a Course Slice

Create a course slice with addCourse and resetCourses reducers.

Task 3: Configure Multiple Slices

Register both student and course reducers in one Redux store.

Task 4: Build a Dashboard

Display student and course counts using useSelector(), and update them using useDispatch().

Summary

The Redux store holds the application's shared state, while slices organize related state and reducer logic for individual features. Redux Toolkit's configureStore() registers reducers, and createSlice() generates reducer logic and action creators. Multiple slices can be combined in one store, and React components can read state using useSelector() and dispatch actions using useDispatch(). Provider makes the store available to the React component tree. In the next lesson, we will learn how to use useSelector and useDispatch in greater detail.