How to handle arrays in query params?

See original GitHub issue

Currently when I update the route from the client using an array value, it returns the following

/search?current_use='family'&current_use='other'

I have been wondering how to use arrays in the query params with react-router, as in:

/search?current_use[]='family'&current_use[]='other'

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
alejandrodnmcommented, Apr 30, 2016

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 query-string package for the parse and stringify functions, doing a quick glance of the code I don’t think it supports your use case, fortunately the qs package does it with just setting an options.

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} />
7reactions
evgenTraytyakcommented, Aug 21, 2016

@alejandrodnm you also need to add { encode: false } to stringify second argument.

const stringifyQuery = query => stringify(query, { arrayFormat: 'brackets', encode: false })

for more explicit view in url param%5B%5D=1&param%5B%5D=10 => param[]=1&param[]=10

Read more comments on GitHub >

github_iconTop Results From Across the Web

Arrays in query params - Medium
Arrays in query params ... The same way there is no concensus over how objects should be represented in query parameters, there is...
Read more >
How to pass an array within a query string? - Stack Overflow
Here's what I figured out: Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, ...
Read more >
Fun stuff: representing arrays and objects in query strings
Let's see how some popular environments handle it. ... Decoding a query string into an array is done with parse_str.
Read more >
Use the Query String to pass an array of selected values ...
You can pass data, including arrays via the Query String when using NavigationManager to navigate to a different page in your Blazor app....
Read more >
Angular - How to pass arrays via Query Parameters
This is a quick guide on how to pass an array of values via query string in Angular. This is working in Angular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found