devtools

devtools middleware lets you use Redux DevTools Extension without Redux. Read more about the benefits of using Redux DevTools for debugging.

WARNING

In order to use devtools from zustand/middleware you need to install the @redux-devtools/extension library.

const nextStateCreatorFn = devtools(stateCreatorFn, devtoolsOptions)

Types

Signature

devtools<T>(stateCreatorFn: StateCreator<T, [], []>, devtoolsOptions?: DevtoolsOptions): StateCreator<T, [['zustand/devtools', never]], []>

Mutator

;['zustand/devtools', never]

Reference

devtools(stateCreatorFn, devtoolsOptions)

Parameters

  • stateCreatorFn: A function that takes set function, get function and store as arguments. Usually, you will return an object with the methods you want to expose.
  • optional devtoolsOptions: An object to define Redux DevTools options.
    • optional name: A custom identifier for the connection in the Redux DevTools.
    • optional enabled: Defaults to true in development mode, and defaults to false in production mode. Enables or disables the Redux DevTools integration for this store.
    • optional anonymousActionType: Defaults to the inferred action type or anonymous if unavailable. A string to use as the action type for anonymous mutations in the Redux DevTools.
    • optional store: A custom identifier for the store in the Redux DevTools.
    • optional actionsDenylist: A string or array of strings (regex patterns) that specify which actions should be filtered out from Redux DevTools. This option is passed directly to Redux DevTools for filtering. For example, ['secret.*'] will filter out all actions starting with "secret".
    • optional actionCreators: An object that picks the functions in the state that can be dispatched from the action dropdown in Redux DevTools. Keys are dot notation paths, where * matches one level and ** matches any depth. Values are true, false to leave a function out, or a function to use instead. See Dispatching actions from the devtools.

Returns

devtools returns a state creator function.

Usage

Debugging a store

This example shows you how you can use Redux DevTools to debug a store.

import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

type JungleStore = {
  bears: number
  addBear: () => void
  fishes: number
  addFish: () => void
}

const useJungleStore = create<JungleStore>()(
  devtools((set) => ({
    bears: 0,
    addBear: () => set((state) => ({ bears: state.bears + 1 }), undefined, 'jungle/addBear'),
    fishes: 0,
    addFish: () => set((state) => ({ fishes: state.fishes + 1 }), undefined, 'jungle/addFish'),
  })),
)

Debugging a Slices pattern based store

This example shows you how you can use Redux DevTools to debug a Slices pattern based store.

import { create, StateCreator } from 'zustand'
import { devtools } from 'zustand/middleware'

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

type FishSlice = {
  fishes: number
  addFish: () => void
}

type JungleStore = BearSlice & FishSlice

const createBearSlice: StateCreator<JungleStore, [['zustand/devtools', never]], [], BearSlice> = (
  set,
) => ({
  bears: 0,
  addBear: () => set((state) => ({ bears: state.bears + 1 }), undefined, 'jungle:bear/addBear'),
})

const createFishSlice: StateCreator<JungleStore, [['zustand/devtools', never]], [], FishSlice> = (
  set,
) => ({
  fishes: 0,
  addFish: () => set((state) => ({ fishes: state.fishes + 1 }), undefined, 'jungle:fish/addFish'),
})

const useJungleStore = create<JungleStore>()(
  devtools((...args) => ({
    ...createBearSlice(...args),
    ...createFishSlice(...args),
  })),
)

Filtering actions with actionsDenylist

You can filter out specific actions from Redux DevTools using the actionsDenylist option. This is useful for hiding internal or sensitive actions from the DevTools timeline.

import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

type Store = {
  user: string | null
  token: string | null
  login: (user: string, token: string) => void
  logout: () => void
  updateData: () => void
}

const useStore = create<Store>()(
  devtools(
    (set) => ({
      user: null,
      token: null,
      login: (user, token) => set({ user, token }, undefined, 'auth/login'),
      logout: () => set({ user: null, token: null }, undefined, 'auth/logout'),
      updateData: () => set({ user: 'updated' }, undefined, 'internal/updateData'),
    }),
    {
      name: 'AuthStore',
      // Filter out actions matching these regex patterns
      actionsDenylist: ['internal/.*'], // Hides all 'internal/*' actions
    },
  ),
)

You can also use a single regex string:

const useStore = create<Store>()(
  devtools(
    (set) => ({
      // ... state and actions
    }),
    {
      name: 'MyStore',
      actionsDenylist: 'secret.*', // Hides all actions starting with 'secret'
    },
  ),
)
NOTE

The actionsDenylist option uses regex pattern matching and is handled directly by Redux DevTools Extension. All actions are still sent to DevTools, but matching actions are filtered from the display.

Dispatching actions from the devtools

With the actionCreators option, functions in your state show up in the action dropdown of Redux DevTools and can be dispatched from there. Type each argument as JSON, like 5, "foo" or { "by": 5 }, and leave it empty for undefined.

import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

type Store = {
  count: number
  actions: {
    increment: (by: number) => void
    reset: () => void
  }
}

const useStore = create<Store>()(
  devtools(
    (set) => ({
      count: 0,
      actions: {
        increment: (by) => set((state) => ({ count: state.count + by }), undefined, 'increment'),
        reset: () => set({ count: 0 }, undefined, 'reset'),
      },
    }),
    {
      name: 'CounterStore',
      // All functions in `actions` except `reset`
      actionCreators: { 'actions.*': true, 'actions.reset': false },
    },
  ),
)

You can also call the same functions by dispatching a custom action in JSON format, passing the arguments as an array or by name:

{ "type": "actions.increment", "args": [5] }
{ "type": "actions.increment", "args": { "by": 5 } }
NOTE

The action dropdown is not supported with the redux middleware. Dispatch a custom action in JSON format instead, like { "type": "increment" }.

NOTE

With the store option, only the first store's functions reach the action dropdown, because later stores reuse its connection. Their functions can still be called with a custom action in JSON format.

Cleanup

When a store is no longer needed, you can clean up the Redux DevTools connection by calling the cleanup method on the store:

import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

type CounterStore = {
  count: number
  increment: () => void
}

const useStore = create<CounterStore>()(
  devtools((set) => ({
    count: 0,
    increment: () => set((state) => ({ count: state.count + 1 })),
  })),
)

// When you're done with the store, clean it up
useStore.devtools.cleanup()

This is particularly useful in applications that wrap stores in context or create multiple stores dynamically.

Troubleshooting

Only one store is displayed

By default, Redux DevTools only shows one store at a time, so in order to see other stores you need to use the store selector and choose a different store.

All action names are labeled as 'anonymous'

If an action type name is not provided, it defaults to 'anonymous'. You can customize this default value by providing an anonymousActionType option.

For instance, the next example doesn't have action type names:

import { create, StateCreator } from 'zustand'
import { devtools } from 'zustand/middleware'

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

type FishSlice = {
  fishes: number
  addFish: () => void
}

type JungleStore = BearSlice & FishSlice

const createBearSlice: StateCreator<JungleStore, [['zustand/devtools', never]], [], BearSlice> = (
  set,
) => ({
  bears: 0,
  addBear: () => set((state) => ({ bears: state.bears + 1 })),
})

const createFishSlice: StateCreator<JungleStore, [['zustand/devtools', never]], [], FishSlice> = (
  set,
) => ({
  fishes: 0,
  addFish: () => set((state) => ({ fishes: state.fishes + 1 })),
})

const useJungleStore = create<JungleStore>()(
  devtools((...args) => ({
    ...createBearSlice(...args),
    ...createFishSlice(...args),
  })),
)

In order to fix the previous example, we need to provide an action type name as the third parameter. Additionally, to preserve the default behavior of the replacement logic, the second parameter should be set to undefined.

Here's the fixed previous example:

import { create, StateCreator } from 'zustand'
import { devtools } from 'zustand/middleware'

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

type FishSlice = {
  fishes: number
  addFish: () => void
}

type JungleStore = BearSlice & FishSlice

const createBearSlice: StateCreator<JungleStore, [['zustand/devtools', never]], [], BearSlice> = (
  set,
) => ({
  bears: 0,
  addBear: () => set((state) => ({ bears: state.bears + 1 }), undefined, 'bear/addBear'),
})

const createFishSlice: StateCreator<JungleStore, [['zustand/devtools', never]], [], FishSlice> = (
  set,
) => ({
  fishes: 0,
  addFish: () => set((state) => ({ fishes: state.fishes + 1 }), undefined, 'fish/addFish'),
})

const useJungleStore = create<JungleStore>()(
  devtools((...args) => ({
    ...createBearSlice(...args),
    ...createFishSlice(...args),
  })),
)
WARNING

Do not set the second parameter to true or false unless you want to override the default replacement logic.