ค้นหาเว็บไซต์

typescript - การเพิ่ม middleware การเก็บข้อมูลถาวรลงในร้านของ zustand


ฉันต้องการเพิ่ม middleware แบบ persist ให้กับ useGlobalStore ของฉัน ฉันต้องการจัดการที่นั่น (ตั้งค่าและดึง) ข้อมูลสำหรับ sessions store ฉันได้ดูเอกสารแล้วแต่ไม่พบคำตอบว่าต้องรวมฟังก์ชัน create และ persist ของฉันเข้าด้วยกันอย่างไร

นี่คือ useGlobalStore ของฉัน:

interface Store extends HeatMapState, WeeklyOverviewState, MonthlyOverviewState, GlobalState, UserState {}

export const useGlobalStore = create<Store>(
    devtools((set, get, api) => ({
        ...heatMapSlice(set as SetState<HeatMapState>, get as GetState<HeatMapState>, api as StoreApi<HeatMapState>),
        ...weeklyOverviewSlice(
            set as SetState<WeeklyOverviewState>,
            get as GetState<WeeklyOverviewState>,
            api as StoreApi<WeeklyOverviewState>,
        ),
        ...monthlyOverviewSlice(
            set as SetState<MonthlyOverviewState>,
            get as GetState<MonthlyOverviewState>,
            api as StoreApi<MonthlyOverviewState>,
        ),
        ...globalStateSlice(set as SetState<GlobalState>, get as GetState<GlobalState>, api as StoreApi<GlobalState>),
        ...userStateSlice(set as SetState<UserState>, get as GetState<UserState>, api as StoreApi<UserState>),
    })),
) as UseBoundStore<Store, StoreApi<Store>>

คำตอบ #1

https://github.com/pmndrs/zustand#middleware

คุณสามารถหามันได้ภายใต้ วิธีการเชื่อมต่อมิดเดิลแวร์

const createStore = pipe(persistor, devtools, create)
create<Store>(....store )

นี่ควรใช้ได้กับกรณีของคุณ

บทความที่เกี่ยวข้อง