UNPKG

@appbuckets/react-ui-core

Version:

Core utilities built for AppBuckets React UI Framework

34 lines (31 loc) 835 B
import * as React from 'react'; import invariant from 'tiny-invariant'; /* -------- * Context Builder * -------- */ function contextBuilder(initialContext, name) { /** Create the base Context */ var BaseContext = React.createContext(initialContext); /** Init the Hook Function */ function useContextHook() { /** Get the value of the context */ var ctxValue = React.useContext(BaseContext); /** Assert value exists */ invariant( ctxValue !== undefined, 'use'.concat( name || 'Context', '() hook must be invoked inside its right Context' ) ); /** Return the Context */ return ctxValue; } /** Return context tools */ return { hook: useContextHook, Provider: BaseContext.Provider, Consumer: BaseContext.Consumer, }; } export { contextBuilder };