daedalOS/contexts/contextFactory.tsx
2023-10-10 22:41:19 -07:00

23 lines
560 B
TypeScript

import { createContext, memo, useContext } from "react";
const contextFactory = <T,>(
useContextState: () => T,
ContextComponent?: React.JSX.Element
): {
Provider: React.MemoExoticComponent<FC>;
useContext: () => T;
} => {
const Context = createContext(Object.create(null) as T);
return {
Provider: memo<FC>(({ children }) => (
<Context.Provider value={useContextState()}>
{children}
{ContextComponent}
</Context.Provider>
)),
useContext: () => useContext(Context),
};
};
export default contextFactory;