import hoistNonReactStatics from 'hoist-non-react-statics'; import React, { Context, useContext } from 'react'; import { Subtract } from 'utility-types'; // -- TYPES interface IWithContextOpts { context: Context; keys?: Array; } // -- MAIN function withContext< TProps extends Partial, TContext extends {}, TComponent extends React.ComponentType = React.ComponentType >({ context, keys }: IWithContextOpts) { return function enhancer(Component: TComponent) { function WithContext(props: Subtract>) { const contextValue = useContext(context); let result: Partial = contextValue; if (keys) { result = {}; for (const key of keys) { result[key] = contextValue[key]; } } // @ts-ignore TODO: resolve type conflict return ; } return hoistNonReactStatics(WithContext, Component); }; } export default withContext;