reactjs - خطأ مع Zustand و Typescript عند استخدام Persist. الخطأ هو: persist.d.ts(95, 210): لم يتم تقديم حجة لـ 'options'
أحاول استخدام وظيفة الاستمرارية باستخدام Zustand باتباع النمط الموجود في وثائق Zustand لـ Typescript، لكنني أتلقى خطأً عند إضافة طريقة persist.
الخطأ هو persist.d.ts(95, 210): لم يتم توفير وسيطة لـ 'options'.
إليك مثال على الكود الذي يسبب الخطأ:
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),
}))
)
);
ما هي هذه "الخيارات" التي أفتقدها وأين يجب تضمينها؟
حاولت البحث عن وثائق حول هذه المشكلة لكنني لم أعثر على أي شيء يحل المشكلة. أقرب مشكلة وجدتها تحتوي على حل باستخدام نسخة قديمة من Zustand.
الإجابة رقم 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
ملعب
