import React from "react" interface ContainerProviderProps { children: React.ReactNode } interface Container { Provider: React.ComponentType useContainer: () => Value } export function createContainer(useHook: () => Value): Container { let Context = React.createContext(null) function Provider(props: ContainerProviderProps) { let value = useHook() return {props.children} } function useContainer(): Value { let value = React.useContext(Context) if (value === null) { throw new Error("Component must be wrapped with ") } return value } return { Provider, useContainer } } export function useContainer(container: Container): Value { return container.useContainer() }