import React from "react" export interface ContainerProviderProps { initialState?: State children: React.ReactNode } export interface Container { Provider: React.ComponentType> useContainer: () => Value } export function createContainer( useHook: (initialState?: State) => Value, ): Container { let Context = React.createContext(null) function Provider(props: ContainerProviderProps) { let value = useHook(props.initialState) 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() }