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
Each slice manages a specific area of the application's state.
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
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
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>
);
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
Increment | Decrement | Reset
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.