UNPKG

@nex-ui/utils

Version:

Utility functions for React components.

33 lines (29 loc) 989 B
"use client"; 'use strict'; var react = require('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 = react.createContext(defaultValue); Context.displayName = contextName; function useContext() { const context = react.useContext(Context); if (!context && strict) { const error = new Error(getErrorMessage(hookName, providerName)); error.name = 'ContextError'; if (Error.captureStackTrace) { Error.captureStackTrace(error, useContext); } throw error; } return context; } return [ Context.Provider, useContext, Context ]; } exports.createContext = createContext;