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
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;
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
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
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
}
}
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
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;
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.