UNPKG

mt-flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

58 lines (57 loc) 1.84 kB
'use client'; import { jsx as _jsx } from "react/jsx-runtime"; import { createContext, useContext, useRef } from 'react'; import { create, useStore } from 'zustand'; import { persist } from 'zustand/middleware'; import { loadDashConfig } from '../mtmapi/mtmapi'; export const createDashSlice = (set, get, initState) => { const DEFAULT_PROPS = {}; return { ...DEFAULT_PROPS, isLoadingDashConfig: false, isOpenNavSheet: false, async loadDashConfig() { set({ ...get(), isLoadingDashConfig: true }); const dashConfig = await loadDashConfig(); set({ ...get(), isLoadingDashConfig: false, dashConfig }); }, setOpenNavSheet: (isOpenNavSheet) => set((s) => ({ isOpenNavSheet, })), ...initState, }; }; export const createDashStore = (initProps) => { return create()(persist((...a) => ({ ...createDashSlice(...a), // ...createFishSlice(...a), }), { name: 'dash', version: 1, // skipHydration: true, onRehydrateStorage(state) { // console.log("onRehydrateStorage[dash]", state) }, })); }; //练习: 合并并且使用中间件。 // export const useDashStore = createDashStore() const Context = createContext(null); export function DashProvider({ children, ...props }) { const storeRef = useRef(); if (!storeRef.current) { storeRef.current = createDashStore(props); } return _jsx(Context.Provider, { value: storeRef.current, children: children }); } export function useDashStore(selector, equals) { const store = useContext(Context); if (!store) throw new Error('Missing DashProvider'); if (selector) { return useStore(store, selector, equals); } else { return store; } }