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
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.
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
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
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.
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
Example State Changes
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;
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.