@vtaits/react-required-context
Version:
React context that has no default value and must be defined to be used
24 lines (21 loc) • 691 B
JavaScript
// src/createContext.ts
import { createContext as createContextBase } from "react";
// src/constants.ts
var DEFAULT_VALUE = Symbol("Required context default value");
var ERROR_MESSAGE = "[react-required-context] `useContext` is used outside of `Provider` of context";
// src/createContext.ts
var createContext = () => createContextBase(DEFAULT_VALUE);
// src/useContext.ts
import { useContext as useContextBase } from "react";
var useContext = (context) => {
const contextValue = useContextBase(context);
if (contextValue === DEFAULT_VALUE) {
throw new Error(ERROR_MESSAGE);
}
return contextValue;
};
export {
createContext,
useContext
};
//# sourceMappingURL=index.js.map