Skip to content
Myr

devtools.md

This file

Path

docs/reference/middlewares/devtools.md

351 lines · 8.8 KiB · Markdown

Last changed by

docs(devtools): add missing devtools import to troubleshooting example (#3501)Seydi Charyyevbefore adoption60a91b4

History of this file

Take it

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 1–30why, one step back
1---
2title: devtools3description: How to time-travel debug your store4nav: 295---67# devtools89`devtools` middleware lets you use [Redux DevTools Extension](https://github.com/reduxjs/redux-devtools)10without Redux. Read more about the benefits of using [Redux DevTools for debugging](https://redux.js.org/style-guide/#use-the-redux-devtools-extension-for-debugging).1112> [!IMPORTANT]13> In order to use `devtools` from `zustand/middleware` you need to install14> `@redux-devtools/extension` library.1516```js17const nextStateCreatorFn = devtools(stateCreatorFn, devtoolsOptions)18```1920- [Types](#types)21 - [Signature](#signature)22 - [Mutator](#mutator)23- [Reference](#reference)24- [Usage](#usage)25 - [Debugging a store](#debugging-a-store)26 - [Debugging a Slices pattern based store](#debugging-a-slices-pattern-based-store)27 - [Filtering actions with actionsDenylist](#filtering-actions-with-actionsdenylist)28 - [Cleanup](#cleanup)29- [Troubleshooting](#troubleshooting)30 - [Only one store is displayed](#only-one-store-is-displayed)

Hotfix section linking (#3410)Copilotbefore adoptionc49df38

line 31why, one step back
31 - [Action names are labeled as 'anonymous'](#all-action-names-are-labeled-as-'anonymous')

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 32–43why, one step back
32
33## Types3435### Signature3637```ts38devtools<T>(stateCreatorFn: StateCreator<T, [], []>, devtoolsOptions?: DevtoolsOptions): StateCreator<T, [['zustand/devtools', never]], []>39```4041### Mutator4243```ts

Upgrade workflow to use build.yml v3 and deploy-pages v4 (#3393)Danilo Brittobefore adoption49ac892

line 44why, one step back
44;['zustand/devtools', never]

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 45–304why, one step back
45```
4647## Reference4849### `devtools(stateCreatorFn, devtoolsOptions)`5051#### Parameters5253- `stateCreatorFn`: A function that takes `set` function, `get` function and `store` as arguments.54 Usually, you will return an object with the methods you want to expose.55- **optional** `devtoolsOptions`: An object to define `Redux Devtools` options.56 - **optional** `name`: A custom identifier for the connection in the Redux DevTools.57 - **optional** `enabled`: Defaults to `true` when is on development mode, and defaults to `false`58 when is on production mode. Enables or disables the Redux DevTools integration59 for this store.60 - **optional** `anonymousActionType`: Defaults to the inferred action type or `anonymous` if61 unavailable. A string to use as the action type for anonymous mutations in the Redux DevTools.62 - **optional** `store`: A custom identifier for the store in the Redux DevTools.63 - **optional** `actionsDenylist`: A string or array of strings (regex patterns) that specify which64 actions should be filtered out from Redux DevTools. This option is passed directly to Redux DevTools65 for filtering. For example, `['secret.*']` will filter out all actions starting with "secret".6667#### Returns6869`devtools` returns a state creator function.7071## Usage7273### Debugging a store7475This example shows you how you can use `Redux Devtools` to debug a store7677```ts78import { create, StateCreator } from 'zustand'79import { devtools } from 'zustand/middleware'8081type JungleStore = {82 bears: number83 addBear: () => void84 fishes: number85 addFish: () => void86}8788const useJungleStore = create<JungleStore>()(89 devtools((set) => ({90 bears: 0,91 addBear: () =>92 set((state) => ({ bears: state.bears + 1 }), undefined, 'jungle/addBear'),93 fishes: 0,94 addFish: () =>95 set(96 (state) => ({ fishes: state.fishes + 1 }),97 undefined,98 'jungle/addFish',99 ),100 })),101)102```103104### Debugging a Slices pattern based store105106This example shows you how you can use `Redux Devtools` to debug a Slices pattern based store107108```ts109import { create, StateCreator } from 'zustand'110import { devtools } from 'zustand/middleware'111112type BearSlice = {113 bears: number114 addBear: () => void115}116117type FishSlice = {118 fishes: number119 addFish: () => void120}121122type JungleStore = BearSlice & FishSlice123124const createBearSlice: StateCreator<125 JungleStore,126 [['zustand/devtools', never]],127 [],128 BearSlice129> = (set) => ({130 bears: 0,131 addBear: () =>132 set(133 (state) => ({ bears: state.bears + 1 }),134 undefined,135 'jungle:bear/addBear',136 ),137})138139const createFishSlice: StateCreator<140 JungleStore,141 [['zustand/devtools', never]],142 [],143 FishSlice144> = (set) => ({145 fishes: 0,146 addFish: () =>147 set(148 (state) => ({ fishes: state.fishes + 1 }),149 undefined,150 'jungle:fish/addFish',151 ),152})153154const useJungleStore = create<JungleStore>()(155 devtools((...args) => ({156 ...createBearSlice(...args),157 ...createFishSlice(...args),158 })),159)160```161162### Filtering actions with actionsDenylist163164You 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.165166```ts167import { create } from 'zustand'168import { devtools } from 'zustand/middleware'169170type Store = {171 user: string | null172 token: string | null173 login: (user: string, token: string) => void174 logout: () => void175 updateData: () => void176}177178const useStore = create<Store>()(179 devtools(180 (set) => ({181 user: null,182 token: null,183 login: (user, token) => set({ user, token }, undefined, 'auth/login'),184 logout: () => set({ user: null, token: null }, undefined, 'auth/logout'),185 updateData: () =>186 set({ user: 'updated' }, undefined, 'internal/updateData'),187 }),188 {189 name: 'AuthStore',190 // Filter out actions matching these regex patterns191 actionsDenylist: ['internal/.*'], // Hides all 'internal/*' actions192 },193 ),194)195```196197You can also use a single regex string:198199```ts200const useStore = create<Store>()(201 devtools(202 (set) => ({203 // ... state and actions204 }),205 {206 name: 'MyStore',207 actionsDenylist: 'secret.*', // Hides all actions starting with 'secret'208 },209 ),210)211```212213> [!NOTE]214> The `actionsDenylist` option uses regex pattern matching and is handled directly by Redux DevTools Extension.215> All actions are still sent to DevTools, but matching actions are filtered from the display.216217### Cleanup218219When a store is no longer needed, you can clean up the Redux DevTools connection by calling the `cleanup` method on the store:220221```ts222import { create } from 'zustand'223import { devtools } from 'zustand/middleware'224225const useStore = create(226 devtools((set) => ({227 count: 0,228 increment: () => set((state) => ({ count: state.count + 1 })),229 })),230)231232// When you're done with the store, clean it up233useStore.devtools.cleanup()234```235236This is particularly useful in applications that wrap store in context or create multiple stores dynamically.237238## Troubleshooting239240### Only one store is displayed241242By default, `Redux Devtools` only show one store at a time, so in order to see other stores you243need to use store selector and choose a different store.244245### All action names are labeled as 'anonymous'246247If an action type name is not provided, it is defaulted to "anonymous". You can customize this248default value by providing a `anonymousActionType` parameter:249250For instance the next example doesn't have action type name:251252```ts253import { create, StateCreator } from 'zustand'254import { devtools } from 'zustand/middleware'255256type BearSlice = {257 bears: number258 addBear: () => void259}260261type FishSlice = {262 fishes: number263 addFish: () => void264}265266type JungleStore = BearSlice & FishSlice267268const createBearSlice: StateCreator<269 JungleStore,270 [['zustand/devtools', never]],271 [],272 BearSlice273> = (set) => ({274 bears: 0,275 addBear: () => set((state) => ({ bears: state.bears + 1 })),276 eatFish: () => set((state) => ({ fishes: state.fishes - 1 })),277})278279const createFishSlice: StateCreator<280 JungleStore,281 [['zustand/devtools', never]],282 [],283 FishSlice284> = (set) => ({285 fishes: 0,286 addFish: () => set((state) => ({ fishes: state.fishes + 1 })),287})288289const useJungleStore = create<JungleStore>()(290 devtools((...args) => ({291 ...createBearSlice(...args),292 ...createFishSlice(...args),293 })),294)295```296297In order to fix the previous example, we need to provide an action type name as the third parameter.298Additionally, to preserve the default behavior of the replacement logic, the second parameter299should be set to `undefined`.300301Here's the fixed previous example302303```ts304import { create, StateCreator } from 'zustand'

docs(devtools): add missing devtools import to troubleshooting example (#3501)Seydi Charyyevbefore adoption60a91b4

line 305why, one step back
305import { devtools } from 'zustand/middleware'

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 306–351why, one step back
306
307type BearSlice = {308 bears: number309 addBear: () => void310}311312type FishSlice = {313 fishes: number314 addFish: () => void315}316317type JungleStore = BearSlice & FishSlice318319const createBearSlice: StateCreator<320 JungleStore,321 [['zustand/devtools', never]],322 [],323 BearSlice324> = (set) => ({325 bears: 0,326 addBear: () =>327 set((state) => ({ bears: state.bears + 1 }), undefined, 'bear/addBear'),328})329330const createFishSlice: StateCreator<331 JungleStore,332 [['zustand/devtools', never]],333 [],334 FishSlice335> = (set) => ({336 fishes: 0,337 addFish: () =>338 set((state) => ({ fishes: state.fishes + 1 }), undefined, 'fish/addFish'),339})340341const useJungleStore = create<JungleStore>()(342 devtools((...args) => ({343 ...createBearSlice(...args),344 ...createFishSlice(...args),345 })),346)347```348349> [!IMPORTANT]350> Do not set the second parameter to `true` or `false` unless you want to override the default351> replacement logic