Pmndrs.docs

Auto Generating Selectors

We recommend using selectors when using either the properties or actions from the store. You can access values from the store like so:

const bears = useBearStore((state) => state.bears)

However, writing these could be tedious. If that is the case for you, you can auto-generate your selectors.

create the following function: createSelectors

import { State, StoreApi, UseBoundStore } from 'zustand'

type WithSelectors<S> = S extends { getState: () => infer T }
  ? S & { use: { [K in keyof T]: () => T[K] } }
  : never

const createSelectors = <S extends UseBoundStore<StoreApi<State>>>(
  _store: S
) => {
  let store = _store as WithSelectors<typeof _store>
  store.use = {}
  for (let k of Object.keys(store.getState())) {
    ;(store.use as any)[k] = () => store((s) => s[k as keyof typeof s])
  }

  return store
}

If you have a store like this:

interface BearState {
  bears: number
  increase: (by: number) => void
  increment: () => void
}

const useBearStoreBase = create<BearState>()((set) => ({
  bears: 0,
  increase: (by) => set((state) => ({ bears: state.bears + by })),
  increment: () => set((state) => ({ bears: state.bears + 1 })),
}))

Apply that function to your store:

const useBearStore = createSelectors(useBearStoreBase)

Now the selectors are auto generated and you can access them directly:

// get the property
const bears = useBearStore.use.bears()

// get the action
const increase = useBearStore.use.increment()

Live Demo

For a working example of this, see the Code Sandbox.

3rd-party Libraries