主题
自动生成选择器
¥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:
typescript
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.
创建以下函数:createSelectors
¥Create the following function: createSelectors
typescript
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:
typescript
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:
typescript
const useBearStore = createSelectors(useBearStoreBase)
现在选择器是自动生成的,你可以直接访问它们:
¥Now the selectors are auto generated and you can access them directly:
typescript
// get the property
const bears = useBearStore.use.bears()
// get the action
const increment = useBearStore.use.increment()
原始存储
¥Vanilla Store
如果你使用的是 vanilla store,请使用以下 createSelectors
函数:
¥If you are using a vanilla store, use the following createSelectors
function:
typescript
import { StoreApi, useStore } from 'zustand'
type WithSelectors<S> = S extends { getState: () => infer T }
? S & { use: { [K in keyof T]: () => T[K] } }
: never
const createSelectors = <S extends StoreApi<object>>(_store: S) => {
const store = _store as WithSelectors<typeof _store>
store.use = {}
for (const k of Object.keys(store.getState())) {
;(store.use as any)[k] = () =>
useStore(_store, (s) => s[k as keyof typeof s])
}
return store
}
用法与 React 存储相同。如果你有这样的存储:
¥The usage is the same as a React store. If you have a store like this:
typescript
import { createStore } from 'zustand'
interface BearState {
bears: number
increase: (by: number) => void
increment: () => void
}
const store = createStore<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:
typescript
const useBearStore = createSelectors(store)
现在选择器是自动生成的,你可以直接访问它们:
¥Now the selectors are auto generated and you can access them directly:
typescript
// get the property
const bears = useBearStore.use.bears()
// get the action
const increment = useBearStore.use.increment()
直播演示
¥Live Demo
有关此示例,请参阅 代码 Sandbox。
¥For a working example of this, see the Code Sandbox.
第三方库
¥Third-party Libraries