frontend/app/lib/get-strict-context.tsx
Nathan Lamy fbb9158684
All checks were successful
Deploy to Netlify / Deploy to Netlify (push) Successful in 1m16s
feat: add stress
2025-12-23 15:15:11 +01:00

36 lines
747 B
TypeScript

import * as React from "react";
function getStrictContext<T>(
name?: string
): readonly [
({
value,
children,
}: {
value: T;
children?: React.ReactNode;
}) => React.JSX.Element,
() => T
] {
const Context = React.createContext<T | undefined>(undefined);
const Provider = ({
value,
children,
}: {
value: T;
children?: React.ReactNode;
}) => <Context.Provider value={value}>{children}</Context.Provider>;
const useSafeContext = () => {
const ctx = React.useContext(Context);
if (ctx === undefined) {
throw new Error(`useContext must be used within ${name ?? "a Provider"}`);
}
return ctx;
};
return [Provider, useSafeContext] as const;
}
export { getStrictContext };