Redux Toolkit
Redux Toolkit covers createSlice, configureStore, Immer-style updates, createAsyncThunk, RTK Query, and focused selectors.
Course: React — State Management Lesson: 06
Learning objectives
Set up Redux Toolkit, create slices, configure a store, connect React components with React Redux, and understand how Redux Toolkit simplifies immutable updates.
Why Redux Toolkit?
Redux Toolkit (RTK) is the recommended approach for writing Redux logic. It provides configureStore, createSlice, createAsyncThunk, and utilities that reduce repetitive code and encourage sound defaults.
Install the packages:
npm install @reduxjs/toolkit react-redux
Create a slice
import { createSlice } from "@reduxjs/toolkit";const cartSlice = createSlice({name: "cart",initialState: { items: [] },reducers: {itemAdded(state, action) {state.items.push(action.payload);},itemRemoved(state, action) {state.items = state.items.filter(item => item.id !== action.payload);},cartCleared(state) {state.items = [];}}});export const { itemAdded, itemRemoved, cartCleared } = cartSlice.actions;export default cartSlice.reducer;
The reducer syntax appears to mutate state. RTK uses Immer internally to produce immutable updates. This convenience does not mean you should mutate objects outside RTK reducers.
Configure the store
import { configureStore } from "@reduxjs/toolkit";import cartReducer from "./cartSlice";export const store = configureStore({reducer: {cart: cartReducer}});
Provide the store
import { Provider } from "react-redux";import { store } from "./store";root.render(<Provider store={store}><App /></Provider>);
Read and update state
import { useDispatch, useSelector } from "react-redux";import { itemAdded } from "./cartSlice";function AddToCartButton({ product }) {const dispatch = useDispatch();const itemCount = useSelector(state => state.cart.items.length);return (<button onClick={() => dispatch(itemAdded(product))}>Add to cart ({itemCount})</button>);}
Selectors should be focused and should avoid returning newly created objects unnecessarily unless memoization is appropriate.
Async workflows
For server communication, RTK offers createAsyncThunk, while RTK Query provides a purpose-built data-fetching and caching solution. Choose the approach based on whether the application needs custom client-state transitions or managed API cache behavior.
Common mistakes
- Creating a store separately inside components.
- Putting non-serializable values into state without a clear need.
- Writing giant slices that combine unrelated domains.
- Selecting the entire root state in every component.
- Treating RTK's reducer mutation syntax as permission to mutate ordinary state elsewhere.
Best practices
Use one configured store, split slices by domain, keep actions meaningful, use selectors, and consider RTK Query for API caching rather than building a custom cache by hand.
Interview questions & answers
Interview preparation
5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.
Beginner
1What does createSlice generate?
BeginnerModel answer
It generates action creators and a reducer for a state slice from a name, initial state, and reducer map.
2Difference between createAsyncThunk and RTK Query?
BeginnerModel answer
createAsyncThunkmodels one async action flow. RTK Query is a dedicated data-fetching/cache layer for endpoints.
Intermediate
3Why can RTK reducer code use mutation-like syntax?
IntermediateModel answer
Immer tracks drafts so you write mutable-looking updates while RTK produces immutable next state underneath.
4Why should selectors be focused?
IntermediateModel answer
Focused selectors limit re-renders to components that care about a specific slice of state.
Advanced
5What is the purpose of configureStore?
AdvancedModel answer
It creates the store with good defaults—reducer combination, middleware, and development checks—without manual boilerplate.
Practice
Create cart and user-preference slices. Configure the store, add a provider, and implement components that dispatch actions and select only the values they need.