-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle arrays in query params? #939
Comments
Hello, I'm migrating an app made with old versions of react and react-router to react 0.15 and react-router 2.0 In the old version the Links where created like this:
This constructed a url like /route/?ids[]=1. That would give me in the component
After the upgrade the Link declaration was changed to:
Which produces urls like /route/ids=1, now the Router parses the querystring like this:
The only way I've managed to get an array if is the array in the link declaration has more than one element, although the url doesn't use empty brackets in the url. So is there a way to force the router to use an array when there is only a single element, I don't want to have to check every time if what I'm getting is an array or a string. |
you can create a custom history, we should probably add an entry to the upgrading guide around here somewhere: https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#using-custom-histories I think you can just do: const history = userRouterHistory(createBrowserHistory)({ parseQuery, stringifyQuery }) Where |
Thx @ryanflorence I'll try that. |
I've run into the same issue after upgrading. An array is only returned if there is more than one element. If there is only one element, then it converts it to a string. I would think that everyone would want to keep the old behavior of maintaining the original data type and always return an array. @alejandrodnm Any luck with the custom history? I would really like to just use a standard history and not have something custom to get around this issue. |
Hi @iamrandys I solved it with the custom history, it's really simple, I posted the answer in this SO question. I'm copy pasting the answer here also, I hope it helps you: As described in the history docs, you need to use a custom history that parses the querystrings like you need. React-router uses the You'll need to do something like this: import { stringify, parse } from 'qs'
import { Router, useRouterHistory } from 'react-router'
import createBrowserHistory from 'history/lib/createBrowserHistory'
const stringifyQuery = query => stringify(query, { arrayFormat: 'brackets' })
const history = useRouterHistory(createBrowserHistory)({ parseQueryString: parse, stringifyQuery })
<Router history={history} /> |
@alejandrodnm you also need to add const stringifyQuery = query => stringify(query, { arrayFormat: 'brackets', encode: false }) for more explicit view in url |
Currently when I update the route from the client using an array value, it returns the following
I have been wondering how to use arrays in the query params with react-router, as in:
The text was updated successfully, but these errors were encountered: