Skip to content

Instantly share code, notes, and snippets.

@createdbymahmood
Created October 2, 2020 22:08
Show Gist options
  • Save createdbymahmood/de6247d02cb01de1b3af0e8ba892544e to your computer and use it in GitHub Desktop.
Save createdbymahmood/de6247d02cb01de1b3af0e8ba892544e to your computer and use it in GitHub Desktop.
react-router routeTo path generator helper 😁
import { generatePath } from "react-router-dom";
import { stringify } from "qs";
import get from "lodash/get";
import __ROUTES__ from "constants/routes";
type PathType = keyof typeof __ROUTES__;
type ParamsType = { [paramName: string]: string | number | boolean };
export const routeTo = (
path: PathType,
params?: ParamsType,
queryString?: object
) => {
const url = generatePath(get(__ROUTES__, path), params);
return queryString ? `${url}?${stringify(queryString)}` : url;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment