# Debug store actions

Use [`devtools`](https://bench-zustand-61.atloria.app/p/bench-zustand-61-pSJCWrWW5Y/developer/zustand-middleware#devtools) to inspect state changes and named actions in Redux DevTools without adding Redux. Give connections and stores distinct identifiers, filter noisy actions from the timeline, and turn off the integration when you do not want to send state to the extension.

This page starts from the project in the [React quick start](https://bench-zustand-61.atloria.app/p/bench-zustand-61-pSJCWrWW5Y/developer/react-quick-start). Keep its entry file, `StrictMode` wrapper, and stylesheet import; the steps below add a debugging screen.

## Debugging a store

### 1. Install the extension support

Install Zustand and the extension package used by the middleware's TypeScript configuration:

```bash
npm install zustand @redux-devtools/extension
```

Install the [Redux DevTools browser extension](https://chromewebstore.google.com/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd), too. The npm package does not install the browser panel. Without the browser extension, your store still works, but no DevTools connection opens.

### 2. Name actions and distinguish stores

Add `src/debug-stores.ts`. Wrap each initializer passed to [`create`](https://bench-zustand-61.atloria.app/p/bench-zustand-61-pSJCWrWW5Y/developer/zustand#create) with `devtools`. The middleware returns a state creator; `create` returns the bound React hook.

The two stores below share the connection name `Jungle` and have different `store` identifiers. Redux DevTools receives their state under the keys `bears` and `fishes`, and prefixes each action with its store identifier.

```ts title="src/debug-stores.ts"
import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

type BearState = {
  bears: number
  addBear: () => void
}

type FishState = {
  fishes: number
  addFishes: (count: number) => void
  clearFish: () => void
}

export const useBearStore = create<BearState>()(
  devtools(
    (set) => ({
      bears: 0,
      addBear: () =>
        set((state) => ({ bears: state.bears + 1 }), undefined, 'addBear'),
    }),
    { name: 'Jungle', store: 'bears', enabled: window.location.hostname === 'localhost' },
  ),
)

export const useFishStore = create<FishState>()(
  devtools(
    (set) => ({
      fishes: 0,
      addFishes: (count) =>
        set(
          (state) => ({ fishes: state.fishes + count }),
          undefined,
          { type: 'addFishes', count },
        ),
      clearFish: () => set({ fishes: 0 }),
    }),
    {
      name: 'Jungle',
      store: 'fishes',
      enabled: window.location.hostname === 'localhost',
      anonymousActionType: 'unnamed',
    },
  ),
)
```

Pass the action name as `set`'s third argument. Keep the second argument `undefined` to preserve the usual replacement logic. An action object carries a `type` and extra payload fields: the fish action includes `count` for inspection.

For updates without an explicit action, `anonymousActionType` supplies the label. Without that option, the middleware tries to infer the caller's name, then falls back to `anonymous`. Explicit action names give you stable labels regardless of stack traces.

Here, `enabled: window.location.hostname === 'localhost'` explicitly limits debugging to pages served on `localhost`. On other hostnames, the stores still update but do not connect to Redux DevTools. This policy checks the hostname, not the build mode; set `enabled: false` to disable debugging regardless of where you serve the app.

## Filtering actions with actionsDenylist

### 1. Add a filtered store

Add `src/activity-store.ts`. Give the store its own connection name, `Activity`, and pass the regex patterns to `actionsDenylist`. The extension filters matching actions from its display; Zustand still applies each update.

```ts title="src/activity-store.ts"
import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

type ActivityState = {
  updates: number
  internalUpdates: number
  update: () => void
  updateInternal: () => void
}

export const useActivityStore = create<ActivityState>()(
  devtools(
    (set) => ({
      updates: 0,
      internalUpdates: 0,
      update: () =>
        set((state) => ({ updates: state.updates + 1 }), undefined, 'activity/update'),
      updateInternal: () =>
        set(
          (state) => ({ internalUpdates: state.internalUpdates + 1 }),
          undefined,
          'internal/updateData',
        ),
    }),
    {
      name: 'Activity',
      enabled: window.location.hostname === 'localhost',
      actionsDenylist: ['^internal/.*'],
    },
  ),
)
```

`actionsDenylist` accepts either one regex string, such as `'^internal/.*'`, or an array of regex strings as shown above. These are strings, not JavaScript `RegExp` objects. With grouped stores, match the prefixed action type, such as `'^fishes/internal/.*'`.

Filtering is not a privacy boundary: all actions and their state are still sent to the extension. Do not use a denylist to protect tokens or other secrets; disable the integration for stores that must not send their contents.

### 2. Mount the debugging screen

Add `src/DebugActions.tsx`. This screen consumes all three stores through selectors and calls their actions from buttons.

```tsx title="src/DebugActions.tsx"
import { useBearStore, useFishStore } from './debug-stores'
import { useActivityStore } from './activity-store'

export default function DebugActions() {
  const bears = useBearStore((state) => state.bears)
  const addBear = useBearStore((state) => state.addBear)
  const fishes = useFishStore((state) => state.fishes)
  const addFishes = useFishStore((state) => state.addFishes)
  const clearFish = useFishStore((state) => state.clearFish)
  const updates = useActivityStore((state) => state.updates)
  const internalUpdates = useActivityStore((state) => state.internalUpdates)
  const update = useActivityStore((state) => state.update)
  const updateInternal = useActivityStore((state) => state.updateInternal)

  return (
    <main style={{ minHeight: 400 }}>
      <h1>Debug store actions</h1>
      <section>
        <h2>Jungle connection</h2>
        <p aria-live="polite">Bears: {bears}; fishes: {fishes}</p>
        <button type="button" onClick={addBear}>Add bear</button>
        <button type="button" onClick={() => addFishes(2)}>Add two fishes</button>
        <button type="button" onClick={clearFish}>Clear fishes</button>
      </section>
      <section>
        <h2>Activity connection</h2>
        <p aria-live="polite">
          Updates: {updates}; internal updates: {internalUpdates}
        </p>
        <button type="button" onClick={update}>Update</button>
        <button type="button" onClick={updateInternal}>Internal update</button>
      </section>
    </main>
  )
}
```

In the quick start's `src/main.tsx`, change the `Counter` import to `import DebugActions from './DebugActions.tsx'` and change `<Counter />` to `<DebugActions />`. Leave the remaining entry code intact.

Build and start the app:

```bash
npm run build
npm run dev
```

### 3. Inspect the results

Open the app on `localhost` to enable the connections in these samples. The page initially shows zero bears, fishes, updates, and internal updates. Open the Redux DevTools panel and select `Jungle` from its connection selector.

- Click **Add bear**: the bear count increases and the timeline shows `bears/addBear`.
- Click **Add two fishes**: the fish count increases by two and `fishes/addFishes` carries the payload field `count: 2`.
- Click **Clear fishes**: the fish count becomes zero and the action is `fishes/unnamed`.

Switch the connection selector to `Activity`. Click **Update** to see `activity/update`. Click **Internal update**: the on-page internal count increases, but `internal/updateData` is filtered from the timeline. Inspect the state associated with a subsequent visible action to see the updated internal count.

You can also jump to an earlier action in Redux DevTools. The middleware applies the selected state back to the store, so subscribed React components display that state.

## Options that matter

Pass these options as the second argument to `devtools`:

| Option | Type | Default | What it does |
| --- | --- | --- | --- |
| `name` | `string` | Unset | Names the connection. Use different names for separate connections. |
| `store` | `string` | Unset | Groups stores with the same connection name under distinct state keys and prefixes their action types. |
| `enabled` | `boolean` | Enabled unless the build mode is `production` | `true` permits a connection if the extension exists; `false` bypasses the integration. |
| `anonymousActionType` | `string` | Inferred caller name, then `anonymous` | Overrides the label used when you omit the third argument to `set`. |
| `actionsDenylist` | `string` or `string[]` | Unset | Passes regex patterns to the extension to filter its displayed actions. |

## Pitfalls and connection lifetime

- Redux DevTools displays one connection at a time. Use its selector before concluding that another store is missing. Within a shared connection, give every `store` a unique identifier.
- Explicitly set `enabled: false` where debugging must be disabled, rather than relying on how a build tool supplies its mode.
- A module-level store remains shared while its consumers mount and unmount. Do not disconnect it every time one consumer unmounts. For a store whose owner actually tears it down, call its `devtools.cleanup()` at that teardown; guard access with `devtools?.cleanup()` because disabled integration or a missing extension leaves that property absent at runtime.
- For middleware composition and typed action labels across slices, continue with [Split a store into slices](https://bench-zustand-61.atloria.app/p/bench-zustand-61-pSJCWrWW5Y/developer/split-store-into-slices). Keep ordinary immutable updates as described in [Immutable state and merging](https://bench-zustand-61.atloria.app/p/bench-zustand-61-pSJCWrWW5Y/developer/immutable-state-and-merging).

## Demo and related guides

Explore the [live Zustand demo](https://zustand-demo.pmnd.rs/) for store-driven React rendering. Use the screen above with the browser extension to inspect the named and filtered actions.

- [Organize store actions](https://bench-zustand-61.atloria.app/p/bench-zustand-61-pSJCWrWW5Y/developer/organize-store-actions) explains how to colocate actions with state.
- [Type stores and selectors](https://bench-zustand-61.atloria.app/p/bench-zustand-61-pSJCWrWW5Y/developer/type-stores-and-selectors) covers typed store creation and middleware-aware signatures.
