Sub topic 15
Inspecting and debugging state with Redux DevTools
This chapter assumes you can already build a store with configureStore, write slices with createSlice, and dispatch actions from React components. It also assumes you have seen createAsyncThunk actions such as pending and fulfilled. Here you will learn how to see what the store is doing and use that to find bugs.
Why DevTools suits Redux
Every state change in Redux is an action passed through a reducer. The store therefore has a complete, ordered history of what happened. Redux DevTools is a browser extension for Chrome, Firefox and Edge. It records that history and shows it as a list of actions, each with the state before and after.
Plain console.log debugging shows one moment at a time. DevTools lets you answer questions such as "which action changed todos.status?" and "what did the state look like three actions ago?"
Turning DevTools on
Install the Redux DevTools extension in your browser. With configureStore, no other setup is needed. The devTools option defaults to true, so the store connects to the extension automatically. Open the browser developer tools and select the Redux tab.
You can pass an options object instead of true to configure the connection:
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from './counterSlice'
export const store = configureStore({
reducer: { counter: counterReducer },
devTools:
process.env.NODE_ENV !== 'production'
? {
name: 'Shop app',
maxAge: 100,
actionsDenylist: ['mouse/moved'],
}
: false,
})These options do the following:
namesets the label in the instance dropdown. It helps when several apps or stores are open at once.maxAgelimits how many actions the extension keeps. The default is 50. Older entries are dropped.actionsDenylisthides noisy action types from the list. The actions still run. Only the display is filtered.- Passing
falsein production turns the connection off. This avoids the memory cost of recording history and stops end users from inspecting your state with the extension.
In a Vite project, use import.meta.env.PROD in place of process.env.NODE_ENV.
Reading the action list
Each entry in the left column is an action type. RTK builds these names for you, which makes the list readable.
| Source | Action type shown |
|---|---|
| `createSlice` named `counter`, reducer `incremented` | `counter/incremented` |
| `createAsyncThunk('todos/fetchTodos', ...)` | `todos/fetchTodos/pending`, then `.../fulfilled` or `.../rejected` |
| RTK Query `createApi` with `reducerPath: 'api'` | `api/executeQuery/pending`, `api/executeQuery/fulfilled` |
If the list shows INIT or @@INIT at the top, that is the store's own initialization action. It is not a bug.
Clicking an entry opens the inspector on the right. Its main tabs are:
- Action shows the full action object, including
typeandpayload. Check here first when a reducer receives the wrong data. - State shows the whole state tree after the selected action. You can expand nodes and search.
- Diff shows only what this action changed. It is the quickest way to confirm that an action touched what you expected and nothing else.
- Trace shows the call stack that dispatched the action. It is empty unless you enable tracing.
- Test generates a test skeleton for the selected action.
Tracing where an action came from
When an action is dispatched from somewhere unexpected, enable trace in the options:
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from './counterSlice'
export const store = configureStore({
reducer: { counter: counterReducer },
devTools: { trace: true, traceLimit: 25 },
})The Trace tab then lists the stack frames, with links to source files. Capturing a stack for every action has a cost, so use this while investigating a problem and remove it afterward.
Time travel
Time travel means moving the displayed state to an earlier point in the action history. You do this in the extension:
- Jump to a past action by clicking the Jump button next to it. The store state is replaced with the state at that point, and the UI re-renders to match.
- Skip an action by clicking its Skip button. The extension recomputes every later action as if the skipped one had never happened.
- Use the slider or the play controls to step through the history and watch the UI change.
Skipping is the useful tool for isolating a bug. If you skip a suspect action and the later state becomes correct, that action or its reducer is the cause.
Time travel only replays reducers. It does not undo side effects. A request already sent by a thunk stays sent, and a value written to localStorage stays written. Stay aware of this when the state you jump to disagrees with what the server holds.
The toolbar at the bottom has more controls:
- Pause stops recording new actions.
- Lock blocks the app from changing state while you inspect it.
- Persist keeps history across page reloads.
- Commit makes the current state the new starting point and clears earlier history.
- Revert returns to the last committed state.
- Import and Export save and load a history as a JSON file. This is useful for attaching a reproduction to a bug report.
The Dispatcher lets you type an action as JSON and send it to the store. You can test a reducer path without building any UI.
Debugging inside reducers
DevTools shows what happened. To see why a reducer produced a value, you sometimes need to log inside it. There is a trap here. Because Immer wraps state in a draft, console.log(state) prints a Proxy object that is hard to read.
Use current from Redux Toolkit to take a plain snapshot:
import { createSlice, current } from '@reduxjs/toolkit'
const cartSlice = createSlice({
name: 'cart',
initialState: { items: [], total: 0 },
reducers: {
itemAdded(state, action) {
console.log('before', current(state))
state.items.push(action.payload)
state.total += action.payload.price
console.log('after', current(state))
},
},
})
export const { itemAdded } = cartSlice.actions
export default cartSlice.reducerRemove these logs when you are done. Reducers should stay free of side effects.
Warnings from the development middleware
In development, configureStore adds two checks that report problems in the browser console. They run only when NODE_ENV is not production.
- The immutability check warns if state was changed outside a reducer, for example
store.getState().items.push(x). Direct mutation hides changes from DevTools and fromuseSelector, so no component re-renders. - The serializability check warns if an action or the state contains a value that cannot be serialized to JSON, such as a
Promise, aMap, a class instance or a function. These values break time travel, persistence and export.
Treat these warnings as bugs and fix the cause. If a non-serializable value is unavoidable in one action, you can ignore that action type specifically:
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from './counterSlice'
export const store = configureStore({
reducer: { counter: counterReducer },
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: ['upload/fileSelected'],
},
}),
})Do not disable the check globally to silence it. You lose the safety net everywhere.
Keeping secrets and bulk out of DevTools
The extension receives your whole state and every action payload. Large or sensitive data causes two problems: a slow panel and exposure of private values. Use sanitizers to replace values before they reach the extension:
import { configureStore } from '@reduxjs/toolkit'
import authReducer from './authSlice'
export const store = configureStore({
reducer: { auth: authReducer },
devTools: {
actionSanitizer: (action) =>
action.type === 'auth/loggedIn'
? { ...action, payload: '<<redacted>>' }
: action,
stateSanitizer: (state) => ({
...state,
auth: { ...state.auth, token: '<<redacted>>' },
}),
},
})Sanitizers change only what DevTools displays. The real store keeps the true values. One side effect is that Jump and Skip operate on sanitized data in the panel, so keep sanitizers to the fields you really need to hide.
A debugging routine
When a piece of UI shows the wrong data, this order usually finds the cause quickly:
- Open the Diff tab for the latest action. Check whether state changed at all.
- If nothing changed, look at the Action tab. A wrong
typeor missingpayloadpoints to the dispatching code. - If nothing was dispatched, the event handler is the problem, not Redux.
- If state is correct in DevTools but the UI is wrong, the issue is in the selector or component. Check the
useSelectorpath and anycreateSelectorinput selectors. - If the state is wrong, use Skip on earlier actions to find which one introduced the bad value. Then add
current(state)logs in that reducer.
Exercise
A teammate says the "Add to cart" button works, but the cart count in the header never changes. In DevTools, the cart/itemAdded action appears in the list, and the State tab shows cart.items containing the new item. Where should you look next, and why?
Show answer
Look at the component that renders the count, not at the reducer. The action ran and the state is correct, so the problem is downstream: probably the useSelector call reads the wrong path, or a createSelector has wrong input selectors or returns a stale result. Also check that the header is inside the Provider for this store.
Exercise
You skip an action in DevTools and the later state becomes correct. Does this prove the fix is to delete that action from your code?
Show answer
No. It shows that this action, as handled by the reducers, produces the wrong later state. The cause may be the dispatching code sending bad data, or a reducer that handles the action incorrectly. Open the Action tab to check the payload, then the reducer logic, before changing anything.