`configureStore` and `createSlice` (Part 1): The Core of Redux Toolkit #114
📖 Introduction
Following our introduction to Redux Toolkit, this article dives into the two core functions that you will use to build your Redux store: configureStore and createSlice. We will learn what these functions do and how they work together to simplify Redux development.
📚 Prerequisites
Before we begin, please ensure you have a solid grasp of the following concepts:
- The core concepts of Redux (actions, reducers, store).
- The benefits of Redux Toolkit.
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅
configureStore: How it simplifies the creation of the Redux store. - ✅
createSlice: How it simplifies the creation of reducers and actions. - ✅ Core Implementation: How to use
configureStoreandcreateSliceto create a simple counter application.
🧠 Section 1: The Core Concepts of configureStore and createSlice
1.1 - configureStore
The configureStore function is a wrapper around the original Redux createStore function that simplifies the setup process. It automatically:
- Combines your slice reducers into a single root reducer.
- Adds the Redux Thunk middleware by default.
- Sets up the Redux DevTools Extension.
1.2 - createSlice
The createSlice function is a powerful utility that allows you to define a "slice" of your Redux state. A slice is a collection of Redux reducer logic and actions for a single feature in your app.
createSlice automatically:
- Generates action creators and action types based on the reducers you define.
- Uses the Immer library internally, which allows you to write "mutating" code that is safely turned into immutable updates.
💻 Section 2: Deep Dive - Implementation and Walkthrough
Let's see how to use configureStore and createSlice to build a simple counter application.
2.1 - Creating a Slice
First, let's create a counterSlice.js file.
// features/counter/counterSlice.js
import { createSlice } from '@reduxjs/toolkit';
export const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: state => {
state.value += 1;
},
decrement: state => {
state.value -= 1;
},
},
});
export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;
Step-by-Step Code Breakdown:
createSlice: We callcreateSlicewith aname, aninitialState, and areducersobject.reducers: Thereducersobject contains the reducer functions for this slice.counterSlice.actions:createSliceautomatically generates action creators for each of our reducer functions.counterSlice.reducer:createSlicealso generates the reducer function for this slice.
2.2 - Configuring the Store
Now, let's create our store in app/store.js.
// app/store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
export default configureStore({
reducer: {
counter: counterReducer,
},
});
Step-by-Step Code Breakdown:
configureStore: We callconfigureStorewith areducerobject.reducer: Thereducerobject contains all of our slice reducers.configureStorewill automatically combine them into a single root reducer.
💡 Conclusion & Key Takeaways
In this article, we've learned about the two core functions of Redux Toolkit: configureStore and createSlice. We've seen how they work together to simplify the process of creating a Redux store and defining your state logic.
Let's summarize the key takeaways:
configureStoresimplifies the creation of the Redux store.createSlicesimplifies the creation of reducers and actions.- Together, they significantly reduce the amount of boilerplate code you need to write.
Challenge Yourself:
To solidify your understanding, try to add a new action to the counterSlice that increments the counter by a specific amount.
➡️ Next Steps
You now have a basic understanding of configureStore and createSlice. In the next article, "configureStore and createSlice (Part 2)", we will build a more practical example of how to use these functions in a React application.
Thank you for your dedication. Stay curious, and happy coding!
glossary
- Slice: A collection of Redux reducer logic and actions for a single feature in your app.
- Immer: A library that allows you to work with immutable state in a more convenient way.