Skip to content

Instantly share code, notes, and snippets.

@radzserg
Created July 10, 2019 09:13
Show Gist options
  • Select an option

  • Save radzserg/e3e39108f3f94bbda296a5e53fb01035 to your computer and use it in GitHub Desktop.

Select an option

Save radzserg/e3e39108f3f94bbda296a5e53fb01035 to your computer and use it in GitHub Desktop.
export default function withLogger<T>(
WrappedComponent: React.ComponentType<T>
): React.ComponentType<T> {
const name = WrappedComponent.displayName || WrappedComponent.name;
class InjectionWrapper extends React.Component<any, any> {
static contextType = LoggerContext;
static displayName = `withLogger(${name})`;
static WrappedComponent = WrappedComponent;
render() {
const logger = this.context as ILogger;
const { ...restProps } = this.props;
return <WrappedComponent logger={logger} {...(restProps as T)} />;
}
}
return InjectionWrapper;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment