@nex-ui/utils
Version:
Utility functions for React components.
31 lines (28 loc) • 997 B
JavaScript
"use client";
import { createContext as createContext$1, useContext } from 'react';
function getErrorMessage(hook, provider) {
return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`;
}
function createContext(options) {
const { defaultValue, contextName, hookName = 'useContext', providerName = 'Provider', strict = true } = options;
const Context = createContext$1(defaultValue);
Context.displayName = contextName;
function useContext$1() {
const context = useContext(Context);
if (!context && strict) {
const error = new Error(getErrorMessage(hookName, providerName));
error.name = 'ContextError';
if (Error.captureStackTrace) {
Error.captureStackTrace(error, useContext$1);
}
throw error;
}
return context;
}
return [
Context.Provider,
useContext$1,
Context
];
}
export { createContext };