import React, { createContext, useContext } from 'react'; const EMPTY = Symbol(); export interface ContainerProviderProps { initialState?: State; children: React.ReactNode; } interface GlobalHook { Provider: React.ComponentType>; useHook: () => Value; } export function createGlobalHook(hook): GlobalHook { const Context = createContext(EMPTY); function Provider(props) { const value = hook(props.initialState); return {props.children}; } function useHook() { const value = useContext(Context); if (value === EMPTY) { throw new Error('You forget to wrap component with'); } return value; } return { Provider, useHook }; }