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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Note URLSearchParams isn't widely supported yet, e.g. in Edge. Please see caniuse.com/#feat=urlsearchparams

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.