UNPKG

mt-flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

82 lines (81 loc) 2.71 kB
'use client'; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { createContext, useEffect, useRef, useContext } from "react"; import { create, useStore } from 'zustand'; import { persist } from 'zustand/middleware'; import { createResViewSlice } from "./resview.slice"; import { subscribeWithSelector } from 'zustand/middleware'; import { devtools } from 'zustand/middleware'; import { createDashSlice } from "./dash.slice"; export const createResAppStore = (initProps) => { return create()(devtools(persist(subscribeWithSelector((...a) => { return { ...createResViewSlice(...a), ...createDashSlice(...a), //初始值, //提示:可以用SSR的方式设置初始值,能覆盖各个Slice中内部的初始值,这样,Slice内部可以不用管初始值的相关部分。 ...initProps, }; }), { name: `resAppStore-${initProps?.resView?.id}`, version: 1, // skipHydration: true, onRehydrateStorage(state) { // console.log("onRehydrateStorage[resView]", state) }, }))); }; //-----------context const Context = createContext(null); export function ResAppProvider({ children, ...props }) { const storeRef = useRef(); if (!storeRef.current) { storeRef.current = createResAppStore(props); } useEffect(() => { storeRef.current?.getState().setParentItemId(props?.parentItemId); }, [ props?.parentItemId ]); useEffect(() => { console.log("props?.depth", props?.depth); storeRef.current?.getState().setDepth(props?.depth); }, [ props?.depth ]); useEffect(() => { console.log("props?.resView", props?.resView); if (props?.resView) { storeRef.current?.getState().setResView(props.resView); } }, [ props?.resView ]); return (_jsxs(Context.Provider, { value: storeRef.current, children: [children, _jsx(SubscriptionSetup, {})] })); } //------------事件订阅 function SubscriptionSetup() { const store = useResStore(); useEffect(() => { // const unsub2 = store.subscribe((state) => state.items, (a) => { // }) return () => { // unsub2() }; }, []); useEffect(() => { store.getState().LoadViewConfig(); }, []); return (_jsx(_Fragment, {})); } export function useResStore(selector, equals) { const store = useContext(Context); if (!store) throw new Error('Missing ResAppProvider'); if (selector) { return useStore(store, selector, equals); } else { return store; } }