我正在try 在zustand中为tauri—apps/plugin—store创建一个自定义存储.当try 更改状态时,控制台中出现错误,

[zustand persist middleware] Unable to update item 'storage', the given storage is currently unavailable.

谁遇到过这种情况或可以帮助?

这是我的存储和存储代码.

import {create} from 'zustand'
import {persist, createJSONStorage, StateStorage} from 'zustand/middleware'
import {Store} from '@tauri-apps/plugin-store';
export const store = new Store('globalStore.bin');

interface MyState {
    bears: number
    addABear: () => void
}

export const useBearStore = create<MyState>()(
    persist((set, get) => ({
            bears: 0,
            addABear: () => set({bears: get().bears + 1}),
        }),
        {
            name: 'storage', // name of item in the storage (must be unique)
            storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
        },
    ),
)

const storage: StateStorage = {
    getItem: async (name: string): Promise<string | null> => {
        console.log(name)
        return (await store.get(name)) || null
    },
    setItem: async (name: string, value: string): Promise<void> => {
        console.log(name, value)
        await store.set(name, value)
    },
    removeItem: async (name: string): Promise<void> => {
        console.log(name)
        await store.delete(name)
    },
}

推荐答案

这里有个working source codewith zustand.

你所要做的就是在useBearStore之上移动storage. 希望有zustand名维护者看到这一点并解决这个问题.

import {create} from 'zustand'
import {persist, createJSONStorage, StateStorage} from 'zustand/middleware'
import {Store} from '@tauri-apps/plugin-store';
export const store = new Store('globalStore.bin');

interface MyState {
    bears: number
    addABear: () => void
}

const storage: StateStorage = {
    getItem: async (name: string): Promise<string | null> => {
        console.log(name)
        return (await store.get(name)) || null
    },
    setItem: async (name: string, value: string): Promise<void> => {
        console.log(name, value)
        await store.set(name, value)
    },
    removeItem: async (name: string): Promise<void> => {
        console.log(name)
        await store.delete(name)
    },
}

export const useBearStore = create<MyState>()(
    persist((set, get) => ({
            bears: 0,
            addABear: () => set({bears: get().bears + 1}),
        }),
        {
            name: 'storage', // name of item in the storage (must be unique)
            storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
        },
    ),
)

Rust相关问答推荐

为什么函数不接受选项T参数的所有权?

在rust中如何修改一个盒装函数并将其赋回?

如何从使用mockall模拟的方法中返回self?

两个相关特征的冲突实现错误

我如何在Rust中使用传递依赖中的特征?

为什么BitVec缺少Serialize trait?

`RwLockWriteGuard_,T`不实现T实现的特征

如何在函数中返回自定义字符串引用?

JSON5中的变量类型(serde)

实现AsyncWrite到hyper Sender时发生生命周期错误

为什么带有生命周期指定的方法不能被调用两次?

为什么 Rust 的临时值有时有参考性有时没有?

具有在宏扩展中指定的生命周期的枚举变体数据类型

是否可以在 Rust 中的特定字符上实现特征?

如何为返回正确类型的枚举实现 get 方法?

Cargo:如何将整个目录或文件包含在功能标志中?

深度嵌套枚举的清洁匹配臂

如何在没有 `make_contiguous()` 的情况下对 VecDeque 进行排序或反转?

Rust 生命周期:不能在方法内重新borrow 可变字段

基于名称是否存在的条件编译