以下模式可用于把状态重置为初始值。
const useSomeStore = create<State & Actions>()((set, get, store) => ({ // your code here reset: () => { set(store.getInitialState()) }, }))
一次性重置多个 store
import type { StateCreator } from 'zustand' import { create: actualCreate } from 'zustand' const storeResetFns = new Set<() => void>() const resetAllStores = () => { storeResetFns.forEach((resetFn) => { resetFn() }) } export const create = (<T>() => { return (stateCreator: StateCreator<T>) => { const store = actualCreate(stateCreator) storeResetFns.add(() => { store.setState(store.getInitialState(), true) }) return store } }) as typeof actualCreate