Sub topic 15
Inspecting and debugging state with Redux DevTools
This chapter assumes you have a working store built with configureStore and at least one slice from createSlice. You should also be comfortable with how actions flow through reducers.
What the DevTools show you
Redux DevTools is a browser extension for Chrome, Firefox and Edge. It records every action your store dispatches, along with the state before and after it. Because reducers are pure functions, that history is enough to replay what happened.
configureStore connects to the extension automatically. If the extension is installed, your store appears in its panel with no further setup.
Reading the panels
Open the browser developer tools and choose the Redux tab. The main panels are:
| Panel | What it shows |
|---|---|
| Action | The payload and type of the selected action |
| State | The full state tree after that action |
| Diff | Only the fields that action changed |
| Trace | The call stack that dispatched the action (needs `trace: true`) |
Action types come from your slices. The incremented reducer in a slice named counter appears as counter/incremented. Async thunks appear as todos/fetchTodos/pending, then fulfilled or rejected. This makes the list read like a log of what your app did.
Time travel
Click an action in the list and the app jumps to the state at that point. You can also skip an action, which replays the history without it. This is useful for reproducing a bug: find the first action where the Diff looks wrong, then check the component or thunk that dispatched it.
Time travel works only if your reducers are pure. A reducer that reads Date.now() or calls Math.random() produces different results on replay.
Configuring the DevTools
Pass the devTools option to configureStore to change the defaults.
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from './counterSlice'
export const store = configureStore({
reducer: { counter: counterReducer },
devTools: import.meta.env.DEV && {
name: 'My App',
maxAge: 50,
trace: true,
},
})namelabels the instance, which helps when several stores run on one page.maxAgelimits how many actions are kept, so long sessions use less memory.tracerecords a stack trace for each action. It costs performance, so enable it only while debugging.
The expression import.meta.env.DEV is Vite's flag for development builds. Other bundlers use process.env.NODE_ENV !== 'production'.
Common problems
- The store does not appear. Check that the extension is installed and that you did not set
devTools: false. A store built with plaincreateStorealso needs extra setup. - Console warnings about non-serializable values. The default middleware flags values such as
Dateobjects, class instances and functions in actions or state. Store ISO strings or timestamps instead. - State changes without an action. Something mutated state outside a reducer. The default immutability check reports this in development, and its message names the mutated path.
- Very large state makes the panel slow. Lower
maxAgeor sanitize big fields.
Exercise
In the DevTools Action list you see todos/fetchTodos/rejected, but the Diff panel shows status still reads 'loading'. Which part of your code do you check first, and why?
Show answer
Check the extraReducers case for fetchTodos.rejected in the todos slice. The action was dispatched, so the thunk ran and failed correctly. State did not change as expected, so the reducer handling that action is either missing the case or not setting status.