UNPKG

state-management-utilities

Version:
20 lines (19 loc) 815 B
import React from "react"; import { center } from "../center"; export function DehydrateStateManager({ pageProps, children, }) { const dehydratedRef = React.useRef({}); React.useEffect(() => { const hydratedIDs = dehydratedRef.current; if (!pageProps?.hydrated || hydratedIDs[pageProps.hydrated.id]) return; center.dehydrate(pageProps.hydrated); hydratedIDs[pageProps.hydrated.id] = true; dehydratedRef.current = hydratedIDs; }, [pageProps?.hydrated]); return (React.createElement(DehydrateContext.Provider, { value: pageProps?.hydrated }, children)); } const DehydrateContext = React.createContext(undefined); export function useDehydrate() { // return React.useContext(DehydrateContext)?.(); return React.useContext(DehydrateContext); }