In this tutorial, we will dive into Redux Toolkit, a powerful set of tools designed to simplify the process of working with Redux. The goal is to reduce the amount of boilerplate code necessary in Redux applications, resulting in a more efficient, streamlined codebase.
You will learn:
Prerequisites:
Redux Toolkit provides several utilities that simplify many Redux patterns. The two main ones we'll be focusing on are configureStore
and createSlice
.
configureStore
simplifies the store setup process. createSlice
automatically generates action creators and action types based on the reducers you define.First, install Redux Toolkit using npm or yarn:
npm install @reduxjs/toolkit
# or
yarn add @reduxjs/toolkit
Now, we can create our store. This is made simple with configureStore
.
import { configureStore } from '@reduxjs/toolkit';
const store = configureStore({
reducer: {
// Your reducers will go here
},
});
A "slice" represents a portion of the state. With createSlice
, you define a name, initial state, and a set of reducer functions, and it gives you back an object with actions and a reducer.
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: state => state + 1,
decrement: state => state - 1,
},
});
// Actions
export const { increment, decrement } = counterSlice.actions;
// Reducer
export default counterSlice.reducer;
Let's create a counter application using Redux Toolkit. We'll have a state that tracks a count, with actions to increment and decrement that count.
Step 1: First, we set up our store and slice.
// store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './counterSlice';
export default configureStore({
reducer: {
counter: counterReducer,
},
});
// counterSlice.js
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: state => state + 1,
decrement: state => state - 1,
},
});
export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;
Step 2: Now, we can use these actions in our React component.
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './counterSlice';
function Counter() {
const count = useSelector(state => state.counter);
const dispatch = useDispatch();
return (
<div>
<button onClick={() => dispatch(increment())}>+</button>
<span>{count}</span>
<button onClick={() => dispatch(decrement())}>-</button>
</div>
);
}
export default Counter;
In this tutorial, we covered the basics of Redux Toolkit, how to set it up, and how to create a slice. We then put these concepts to use in a practical example of a counter application.
Next, you could try adding more complex state and actions to your Redux store. Or, check out the official Redux Toolkit documentation for more information and examples.
Create a todo list application using Redux Toolkit. The state should be an array of todos, each with a text
and completed
property. Actions should include addTodo
, toggleTodo
, and deleteTodo
.
Add a filter to the todo list application. The state should include a filter
value, and actions should include setFilter
. The list of todos should change based on the filter.
For solutions and detailed explanations, please refer to this GitHub repository. Keep practicing and happy coding!