Created
January 28, 2019 00:41
-
-
Save sibelius/dc349e93eeff66252a859f54027078d4 to your computer and use it in GitHub Desktop.
routeTo helper to handle params and query string
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 { 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` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are missing a parenthesis on line 7 behind
stringify(qs
.It should be
return qs ? `${url}?${stringify(qs)}` : url;
Cheers,