英文:
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 '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.
答案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
英文:
Provide options for persist
(it's in the error; "persist.d.ts"):
export const useCountryStore = create<CountrySlice>()(
persist(
devtools((...a) => ({
...countrySlice(...a),
})),
{ name: "my cool name" },
)
);
The name is required.
See the docs: https://docs.pmnd.rs/zustand/integrations/persisting-store-data
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论