Created
May 16, 2017 19:17
-
-
Save mikebridge/3b1746a3208e0eb6bc3e1a00275ad941 to your computer and use it in GitHub Desktop.
query string HOC for react-router 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hope to get this one written in plain JS with class rather than typescript.