import React from 'react' ; function createContext < ContextValueType extends object > ( rootComponentName ) { const Context = React . createContext < ContextValueType > ( null as any ) ; function Provider ( props ) { const { children , ... providerProps } = props ; const value = React . useMemo ( ( ) => providerProps , Object . values ( providerProps ) ) as ContextValueType ; return < Context . Provider value = { value } > { children } < /Context.Provider>; } function useContext ( consumerName ) { const context = React . useContext ( Context ) ; if ( context === null ) { throw new Error ( `\`${ consumerName }\` must be used within \`${ rootComponentName }\`` ) ; } return context ; } Provider . displayName = rootComponentName + 'Provider' ; return [ Provider , useContext ] as const ; } export { createContext } ;