zustand/docs/guides/auto-generating-selectors.md
Blazej Sewera b1cea640c8
Revise the third-party libraries documentation page (#1478)
* docs(#1220): third-party libraries page revision

- Make Zustand name always start from a capital letter,
- Unify list formatting and description style,
- Change minuses to em-dashes,
- Change 3rd to third.

* docs(#1220): update links from 3rd-party to third-party
2022-12-15 12:26:09 +09:00

2.0 KiB

title nav
Auto Generating Selectors 6

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 { 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<object>>>(
  _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.

Third-party Libraries