import { createContext, use } from "react"; /** * Creates context values along with a pre-bound use(Context) hook that throws * if context value is falsy. * * @param [debugName="Value"] Optionally, provide a debug name for this context * value to be used within error messages if context value is nullish. * * @example * ```ts * export const [KnightContext, useKnight] = createStrictContext("KnightContext"); * ``` */ export function createStrictContext(debugName: string = "Value") { const Context = createContext(null); const useStrictContext = () => { const value = use(Context); if (!value) { throw new Error(`${debugName} not found in context.`); } return value; }; return [Context, useStrictContext] as const; }