Skip to content

Instantly share code, notes, and snippets.

@MrDesjardins
Last active September 13, 2017 15:46
Show Gist options
  • Save MrDesjardins/8d7429c405a80a059452654d2c2866ac to your computer and use it in GitHub Desktop.
Save MrDesjardins/8d7429c405a80a059452654d2c2866ac to your computer and use it in GitHub Desktop.
React-Router HOC
import * as React from "react";
export function withLog<P>(Comp: React.ComponentClass<P> | React.StatelessComponent<P>): React.ComponentClass<P> {
return class WrappedComponent extends React.Component<P, {}> {
public render(): JSX.Element {
console.log("render");
return <Comp {...this.props} />;
}
public componentDidMount() {
console.log("componentDidMount");
}
public componentWillMount() {
console.log("componentWillMount");
}
public componentDidUpdate() {
console.log("componentDidUpdate");
}
};
}
@MrDesjardins
Copy link
Author

This is used this way:

<Route path={Routes.ABC} component={withLog(AbcComponent)} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment