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评论103阅读模式
英文:

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:

  1. import { create, StateCreator } from 'zustand';
  2. import { devtools, persist } from 'zustand/middleware';
  3. import { CountryData } from './countryTypes';
  4. interface CountrySlice {
  5. countries: CountryData[];
  6. fetchCountries: () => void;
  7. }
  8. const countrySlice: StateCreator<CountrySlice> = (set) => ({
  9. countries: [],
  10. fetchCountries: async () => {
  11. const res = await fetch('https://restcountries.com/v3.1/all');
  12. set({ countries: await res.json() });
  13. },
  14. });
  15. export const useCountryStore = create<CountrySlice>()(
  16. persist(
  17. devtools((...a) => ({
  18. ...countrySlice(...a),
  19. }))
  20. )
  21. );

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:

  1. import { create, StateCreator } from &#39;zustand&#39;;
  2. import { devtools, persist } from &#39;zustand/middleware&#39;;
  3. import { CountryData } from &#39;./countryTypes&#39;;
  4. interface CountrySlice {
  5. countries: CountryData[];
  6. fetchCountries: () =&gt; void;
  7. }
  8. const countrySlice: StateCreator&lt;CountrySlice&gt; = (set) =&gt; ({
  9. countries: [],
  10. fetchCountries: async () =&gt; {
  11. const res = await fetch(&#39;https://restcountries.com/v3.1/all&#39;);
  12. set({ countries: await res.json() });
  13. },
  14. });
  15. export const useCountryStore = create&lt;CountrySlice&gt;()(
  16. persist(
  17. devtools((...a) =&gt; ({
  18. ...countrySlice(...a),
  19. }))
  20. )
  21. );

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"):

  1. export const useCountryStore = create<CountrySlice>()(
  2. persist(
  3. devtools((...a) => ({
  4. ...countrySlice(...a),
  5. })),
  6. { name: "my cool name" },
  7. )
  8. );

名称是必需的。

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

Playground

英文:

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

  1. export const useCountryStore = create&lt;CountrySlice&gt;()(
  2. persist(
  3. devtools((...a) =&gt; ({
  4. ...countrySlice(...a),
  5. })),
  6. { name: &quot;my cool name&quot; },
  7. )
  8. );

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:

确定