import React from 'react'; import {IRendererStore} from './store'; import hoistNonReactStatic from 'hoist-non-react-statics'; export const RootStoreContext = React.createContext( undefined as any ); export function withRootStore< T extends React.ComponentType< React.ComponentProps & { rootStore: IRendererStore; } > >(ComposedComponent: T) { type OuterProps = JSX.LibraryManagedAttributes< T, Omit, 'rootStore'> >; const result = hoistNonReactStatic( class extends React.Component { static displayName = `WithRootStore(${ ComposedComponent.displayName || ComposedComponent.name })`; static contextType = RootStoreContext; static ComposedComponent = ComposedComponent; render() { const rootStore = this.context; const injectedProps: { rootStore: IRendererStore; } = { rootStore }; return ( >)} {...injectedProps} /> ); } }, ComposedComponent ); return result as typeof result & { ComposedComponent: T; }; }