import React from "react"; export const createInverseContext = ( initialValue: T, ) => { const Context = React.createContext(initialValue) as React.Context & { _updateProviderValue?: (value: T) => void; }; class InverseConsumer extends React.Component { state = { value: initialValue }; constructor(props: any) { super(props); Context._updateProviderValue = (value: T) => this.setState({ value }); } render() { return ( {this.props.children} ); } } class InverseProvider extends React.Component<{ value: T }> { componentDidMount() { Context._updateProviderValue?.(this.props.value); } componentDidUpdate() { Context._updateProviderValue?.(this.props.value); } render() { return {() => this.props.children}; } } return { Context, Consumer: InverseConsumer, Provider: InverseProvider, }; };