Error with Zustand and Typescript when using Persist. The Error is: persist.d.ts(95, 210): An argument for 'options' was not provided

huangapple go评论73阅读模式
英文:

Error with Zustand and Typescript when using Persist. The Error is: persist.d.ts(95, 210): An argument for 'options' was not provided

问题

以下是您要翻译的部分:

"I'm trying to use the persist functionality using Zustand following the pattern on the Zustand documentation for Typescript but I get an error when I add the persist method.

The error is persist.d.ts(95, 210): An argument for 'options' was not provided.

Here is an example of the code producing the error:

import { create, StateCreator } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { CountryData } from './countryTypes';

interface CountrySlice {
    countries: CountryData[];
    fetchCountries: () => void;
}

const countrySlice: StateCreator<CountrySlice> = (set) => ({
    countries: [],
    fetchCountries: async () => {
        const res = await fetch('https://restcountries.com/v3.1/all');
        set({ countries: await res.json() });
    },
});

export const useCountryStore = create<CountrySlice>()(
    persist(
        devtools((...a) => ({
            ...countrySlice(...a),
        }))
    )
);

What are these "options" I'm missing and where should they be included?

I tried finding documentation on this issue but haven't found anything that fixes the issue. The closest issue I found has a solution using a deprecated version of Zustand."

英文:

I'm trying to use the persist functionality using Zustand following the pattern on the Zustand documentation for Typescript but I get an error when I add the persist method.

The error is persist.d.ts(95, 210): An argument for &#39;options&#39; was not provided.

Here is an example of the code producing the error:

import { create, StateCreator } from &#39;zustand&#39;;
import { devtools, persist } from &#39;zustand/middleware&#39;;
import { CountryData } from &#39;./countryTypes&#39;;

interface CountrySlice {
	countries: CountryData[];
	fetchCountries: () =&gt; void;
}

const countrySlice: StateCreator&lt;CountrySlice&gt; = (set) =&gt; ({
	countries: [],
	fetchCountries: async () =&gt; {
		const res = await fetch(&#39;https://restcountries.com/v3.1/all&#39;);
		set({ countries: await res.json() });
	},
});


export const useCountryStore = create&lt;CountrySlice&gt;()(
	persist(
		devtools((...a) =&gt; ({
			...countrySlice(...a),
		}))
	)
);

What are these "options" I'm missing and where should they be included?

I tried finding documentation on this issue but haven't found anything that fixes the issue. The closest issue I found has a solution using a deprecated version of Zustand.

答案1

得分: 1

persist 提供选项(它在错误信息中; "persist.d.ts"):

export const useCountryStore = create<CountrySlice>()(
    persist(
        devtools((...a) => ({
            ...countrySlice(...a),
        })),
        { name: "my cool name" },
    )
);

名称是必需的。

查看文档:https://docs.pmnd.rs/zustand/integrations/persisting-store-data

Playground

英文:

Provide options for persist (it's in the error; "persist.d.ts"):

export const useCountryStore = create&lt;CountrySlice&gt;()(
    persist(
        devtools((...a) =&gt; ({
            ...countrySlice(...a),
        })),
        { name: &quot;my cool name&quot; },
    )
);

The name is required.

See the docs: https://docs.pmnd.rs/zustand/integrations/persisting-store-data

Playground

huangapple
  • 本文由 发表于 2023年3月1日 14:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定