Sub topic 12
Inspecting state with Redux DevTools
This chapter shows how to connect a Zustand store to the Redux DevTools browser extension. It assumes you can create a store with create, write actions with set, and know that middleware wraps the state creator, as covered in the earlier chapters.
Why inspect state
Zustand updates happen inside set calls. When a value looks wrong, you need to know which action changed it and what the state was before. The devtools middleware sends each update to the Redux DevTools extension. You can then see a list of updates, compare the state before and after each one, and step back through earlier states.
You need the Redux DevTools extension installed in Chrome, Firefox, or Edge. Without the extension, the middleware does nothing and the store works as normal.
Adding devtools
Import devtools from zustand/middleware and wrap the state creator. Pass an options object as the second argument.
import { create } from 'zustand'
import { devtools } from 'zustand/middleware'
interface CounterState {
count: number
increment: () => void
reset: () => void
}
export const useCounterStore = create<CounterState>()(
devtools(
(set) => ({
count: 0,
increment: () =>
set((state) => ({ count: state.count + 1 }), undefined, 'counter/increment'),
reset: () => set({ count: 0 }, undefined, 'counter/reset'),
}),
{ name: 'CounterStore' },
),
)The name option sets the label of the store in the extension's instance list. This matters when a page has several stores.
Naming actions
devtools adds a third argument to set. The first argument is the update, as before. The second is the replace flag, which you leave as undefined to merge. The third is the action name shown in the DevTools log.
If you leave the name out, every entry appears as anonymous. With many updates, that makes the log hard to read. A sliceName/actionName pattern keeps entries easy to scan.
Using the extension
Open your browser's developer tools and select the Redux tab. Pick your store from the instance list. Then:
- Click an entry in the log to see the state after that update.
- Use the Diff view to see which fields changed.
- Use the slider or the jump controls to move back to an earlier state.
Time travel in the extension changes the displayed state. Treat it as a debugging aid and not as a feature of your app.
Combining with other middleware
Middleware composes by nesting. Put devtools on the outside, as in the persist example from the previous chapter, so it sees updates from the layers inside it.
export const useCartStore = create<CartState>()(
devtools(
persist(stateCreator, { name: 'cart-storage' }),
{ name: 'CartStore' },
),
)Common problems
- The store does not appear. Check that the extension is installed and that the store has been created. A module that is never imported never creates its store.
- Entries are called anonymous. Pass an action name as the third argument to
set. - Production builds. State sent to the extension can include sensitive data. Pass
enabled: falsein the options to turn it off in production, for exampleenabled: import.meta.env.DEVin a Vite project.
Exercise
A store uses devtools, but every entry in the Redux DevTools log is labelled anonymous. What do you change, and where?
Show answer
Pass an action name as the third argument to set, for example set({ count: 0 }, undefined, 'counter/reset'). The second argument stays undefined so that the update still merges into the state.