0

After switching to react-router 4.0 I dumbly wasted way too much time trying to figure out how to get and set the location.search string to a regular query object.

Even after finally realizing they're not handling query params anymore (Parse Query Parameters in React Router v4) and turning to URLSearchParams I then couldn't remember how to easily extract an object from an iterator. Answer below. Hope it helps someone.

Sigfried
  • 2,205
  • 2
  • 24
  • 39

1 Answers1

0

At least here's a pretty short way to do it with lodash:

const getQuery = () => {
  let qp = new URLSearchParams(location.search)
  _.fromPairs([...qp.entries()])
}

Oh, and now, on submitting I see: How to parse query string in react-router v4, but my answer gets the whole object.

Community
  • 1
  • 1
Sigfried
  • 2,205
  • 2
  • 24
  • 39
  • Note URLSearchParams isn't widely supported yet, e.g. in Edge. Please see http://caniuse.com/#feat=urlsearchparams – Ray Shan Aug 15 '17 at 17:57