如何坚持Zustand,将动作与状态分开?

问题描述 投票:0回答:1

基于此示例:https://doichevkostia.dev/blog/authentication-store-with-zustand/

当我尝试从

persist
添加
Zustand
中间件时,商店中
actions
属性内的所有操作都会停止工作,例如,当我尝试使用它们时,会抛出
setAccessToken is not a function

调试我意识到主要问题是当我添加

persist
中间件时,正如我所说。 我认为问题出在以下声明的类型上:


/**
 * Required for zustand stores, as the lib doesn't expose this type
 */
export type ExtractState<S> = S extends {
    getState: () => infer T;
}
    ? T
    : never;

type Params<U> = Parameters<typeof useStore<typeof authStore, U>>;

但实际上我不知道如何修改此类型以使用

persist
中间件。

我错了吗?我现在有点迷失了,我不知道应该做什么才能让这个例子与

persist
一起使用。我一直在查看官方文档,但不知道该怎么做。

reactjs typescript react-hooks zustand
1个回答
0
投票

https://stackblitz.com/edit/vitejs-vite-fhnete?file=src%2Fauth-store.ts

不要坚持该行为

 partialize: (state) => {
          return omit(state, ['actions']);
        },
© www.soinside.com 2019 - 2024. All rights reserved.