React Tutorial 0/120 lessons ~6 min read Lesson 66

    Redux Toolkit

    Redux Toolkit covers createSlice, configureStore, Immer-style updates, createAsyncThunk, RTK Query, and focused selectors.

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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:

    bash
    npm install @reduxjs/toolkit react-redux

    Create a slice

    javascript
    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

    javascript
    import { configureStore } from "@reduxjs/toolkit";
    import cartReducer from "./cartSlice";
    export const store = configureStore({
    reducer: {
    cart: cartReducer
    }
    });

    Provide the store

    javascript
    import { Provider } from "react-redux";
    import { store } from "./store";
    root.render(
    <Provider store={store}>
    <App />
    </Provider>
    );

    Read and update state

    javascript
    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

    2
    1. 1What does createSlice generate?
      Beginner

      Model answer

      It generates action creators and a reducer for a state slice from a name, initial state, and reducer map.

    2. 2Difference between createAsyncThunk and RTK Query?
      Beginner

      Model answer

      createAsyncThunk models one async action flow. RTK Query is a dedicated data-fetching/cache layer for endpoints.

    Intermediate

    2
    1. 3Why can RTK reducer code use mutation-like syntax?
      Intermediate

      Model answer

      Immer tracks drafts so you write mutable-looking updates while RTK produces immutable next state underneath.

    2. 4Why should selectors be focused?
      Intermediate

      Model answer

      Focused selectors limit re-renders to components that care about a specific slice of state.

    Advanced

    1
    1. 5What is the purpose of configureStore?
      Advanced

      Model 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.

    Ready to mark this lesson complete?Track your journey across the entire course.