Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created January 28, 2019 00:41
Show Gist options
  • Save sibelius/dc349e93eeff66252a859f54027078d4 to your computer and use it in GitHub Desktop.
Save sibelius/dc349e93eeff66252a859f54027078d4 to your computer and use it in GitHub Desktop.
routeTo helper to handle params and query string
import { generatePath } from 'react-router-dom';
import { stringify } from 'query-string';
const routeTo = (path: string, params: object, qs: object) => {
const url = generatePath(path, params);
return qs ? `${url}?${stringify(qs}` : url;
}
history.push(routeTo('admin/view/:id', { id: 'ok'}, { anotherParam: 'value' }));
// resulting route
`/admin/view/ok?anotherParam=value`
@R4YM3
Copy link

R4YM3 commented May 21, 2021

You are missing a parenthesis on line 7 behind stringify(qs.

It should be
return qs ? `${url}?${stringify(qs)}` : url;

Cheers,

@sibelius
Copy link
Author

Tks

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