ReactMedium

What is Redux Toolkit?

Redux Toolkit (RTK) is the official, recommended way to write Redux. It provides configureStore (good defaults, DevTools, thunk middleware), createSlice (reducers and action creators together, with Immer for "mutating" syntax), createAsyncThunk, createEntityAdapter, the listener middleware, and RTK Query for data fetching and caching.

Main APIs

  • configureStore: combines slice reducers, adds the thunk middleware, enables Redux DevTools, and adds dev checks for mutations and non-serializable values.
  • createSlice: define name, initialState and reducers; get actions and the reducer automatically. Immer lets you write state.items.push(x) safely.
  • createAsyncThunk: dispatches pending, fulfilled and rejected actions around an async function; handle them in extraReducers.
  • createEntityAdapter: normalized collections with ready-made CRUD reducers and selectors.
  • createListenerMiddleware: run side effects in response to actions (a lighter alternative to sagas).
  • createSelector (from Reselect) for memoized derived data.
  • RTK Query (createApi): declares endpoints and generates hooks (useGetPostsQuery, useAddPostMutation) with caching, deduplication, invalidation tags and polling.

Why it exists

Classic Redux required lots of boilerplate and made it easy to mutate state accidentally. RTK encodes the Redux team's best practices.

TypeScript

Infer RootState and AppDispatch from the store and create typed hooks: useAppSelector = useSelector.withTypes<RootState>().

Version

RTK 2.x (with React Redux 9.x) supports React 18 and 19 and ships modern ESM builds.

Code example

tsx
1import { configureStore, createAsyncThunk, createSlice } from "@reduxjs/toolkit";
2import { useDispatch, useSelector } from "react-redux";
3
4type Post = { id: number; title: string };
5
6export const fetchPosts = createAsyncThunk("posts/fetch", async () => {
7 const res = await fetch("/api/posts");
8 return (await res.json()) as Post[];
9});
10
11const postsSlice = createSlice({
12 name: "posts",
13 initialState: { items: [] as Post[], status: "idle" as "idle" | "loading" | "failed" },
14 reducers: {
15 postAdded(state, action: { payload: Post }) {
16 state.items.push(action.payload); // Immer: safe "mutation"
17 },
18 },
19 extraReducers: (builder) => {
20 builder
21 .addCase(fetchPosts.pending, (s) => void (s.status = "loading"))
22 .addCase(fetchPosts.fulfilled, (s, a) => {
23 s.status = "idle";
24 s.items = a.payload;
25 })
26 .addCase(fetchPosts.rejected, (s) => void (s.status = "failed"));
27 },
28});
29
30export const store = configureStore({ reducer: { posts: postsSlice.reducer } });
31export type RootState = ReturnType<typeof store.getState>;
32export const useAppSelector = useSelector.withTypes<RootState>();
33export const useAppDispatch = useDispatch.withTypes<typeof store.dispatch>();
34export const { postAdded } = postsSlice.actions;

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app