๐ป Bear necessities for state management in React
Install
bashnpm install zustand
Install these too only if you use what needs them: @types/react, immer, react, use-sync-external-store.
Functions
useStore
tsfunction useStore<S extends ReadonlyStoreApi<unknown>>(
api: S,
): ExtractState<S>
function useStore<S extends ReadonlyStoreApi<unknown>, U>(
api: S,
selector: (state: ExtractState<S>) => U,
): U
Constants
create
tsconst create: Create
createStore
tsconst createStore: CreateStore
Interfaces
StoreApi
tsinterface StoreApi<T>
Properties
| Name | Type | Default | Description |
|---|---|---|---|
setState | SetStateInternal<T> | ||
getState | () => T | ||
getInitialState | () => T | ||
subscribe | (listener: (state: T, prevState: T) => void) => () => void |
StoreMutators
Also has every member of StoreMutators, listed on its own entry.
tsinterface StoreMutators<S, A>
Types
ExtractState
tstype ExtractState<S> = S extends { getState: () => infer T } ? T : never
Mutate
tstype Mutate<S, Ms> = number extends Ms['length' & keyof Ms]
? S
: Ms extends []
? S
: Ms extends [[infer Mi, infer Ma], ...infer Mrs]
? Mutate<StoreMutators<S, Ma>[Mi & StoreMutatorIdentifier], Mrs>
: never
StateCreator
tstype StateCreator<
T,
Mis extends [StoreMutatorIdentifier, unknown][] = [],
Mos extends [StoreMutatorIdentifier, unknown][] = [],
U = T,
> = ((
setState: Get<Mutate<StoreApi<T>, Mis>, 'setState', never>,
getState: Get<Mutate<StoreApi<T>, Mis>, 'getState', never>,
store: Mutate<StoreApi<T>, Mis>,
) => U) & { $$storeMutators?: Mos }
Properties
| Name | Type | Default | Description |
|---|---|---|---|
$$storeMutators? | Mos |
StoreMutatorIdentifier
Also has every member of String, listed on its own entry.
tstype StoreMutatorIdentifier = keyof StoreMutators<unknown, unknown>
UseBoundStore
Also has every member of StoreApi, listed on its own entry.
tstype UseBoundStore<S extends ReadonlyStoreApi<unknown>> = {
(): ExtractState<S>
<U>(selector: (state: ExtractState<S>) => U): U
} & S
Was this page helpful?