Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Created May 16, 2017 19:17
Show Gist options
  • Save mikebridge/3b1746a3208e0eb6bc3e1a00275ad941 to your computer and use it in GitHub Desktop.
Save mikebridge/3b1746a3208e0eb6bc3e1a00275ad941 to your computer and use it in GitHub Desktop.
query string HOC for react-router 4
import * as React from "react";
type HOCWrapped<PWrapped, PHoc> = React.ComponentClass<PWrapped & PHoc> | React.SFC<PWrapped & PHoc>;
const queryString = require("query-string");
import {RouteComponentProps, withRouter} from "react-router";
export interface IQueryStringProps {
params: any;
}
type IWithQueryStringProps=
RouteComponentProps<any> &
IQueryStringProps;
export function withQueryString<P, S>(Component: HOCWrapped<P, IQueryStringProps &
IQueryStringProps>): React.ComponentClass<P> {
class C extends React.Component<P & IWithQueryStringProps, S> {
public render(): JSX.Element {
// don't propagate the react-router props
const {match, location, history, staticContext, ...rest} = this.props as any;
const params = queryString.parse(this.props.location.search);
return (
<Component params={params} {...rest} />
);
}
}
return withRouter(C);
}
export default withQueryString;
@onurarpapay
Copy link

I hope to get this one written in plain JS with class rather than typescript.

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