Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Last active January 18, 2018 09:01
Show Gist options
  • Save mikebridge/96a4487aa185343c13bfba5fcc3b431b to your computer and use it in GitHub Desktop.
Save mikebridge/96a4487aa185343c13bfba5fcc3b431b to your computer and use it in GitHub Desktop.
Example typescript wrapper around react-router 4 for blog entry
import * as React from "react";
import {RouteComponentProps, withRouter} from "react-router";
export interface IWithNavigationProps {
navigate: () => void;
}
export function withNavigation<P, S>(
Component: React.ComponentClass<P & IWithNavigationProps> | React.SFC<P & IWithNavigationProps>,
routeWhenClicked: string): React.ComponentClass<P> {
class C extends React.Component<P & RouteComponentProps<P>, S> {
constructor(props: P & RouteComponentProps<P>) {
super(props);
this.onClick = this.onClick.bind(this);
}
onClick = () => {
this.props.history.push(routeWhenClicked);
}
public render(): JSX.Element {
console.log("RENDERING WITH ");
console.log(this.props);
const {onClick, passThroughProps} = this.props as any;
return (
<Component navigate={this.onClick} {...passThroughProps} />
);
}
}
return withRouter(C);
}
export default withNavigation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment