Hacker News new | ask | show | jobs
by shepmaster 3432 days ago
Will there be a way to access query / search parameters? The URL Parameters example could benefit from a such an addition, if it's already possible. Something showing how to get the genre out of `/netflix?genre=mystery` would be lovely.
3 comments

I believe what you're looking for is `location.query` which can be accessed by children of your router (via `this.props.location.query`)
Thank you, I'll give that a try. The closest I saw in the current docs was via the `history` object, which I'd then have to provide to `<Router>`, which seemed sad. Using the props makes more sense! It looks like I want to use `withRouter` to get access to the location prop.
you get location.search, which is just a string. We removed default query handling because it can be composed in with something like `RouteWithQuery` and `LinkWithQuery`. Lots of people didn't want the default serialization anyway. It's like 7 lines, we'll have an example of it you can copy/paste.
Actually it's`match.params.genre` with a route like this `<Route path="/:genre" component={Genre}/>` You can see it in these examples https://reacttraining.com/react-router/examples/url-params https://reacttraining.com/react-router/examples/ambiguous-ma...
I'm not sure I understand you. I'm asking for query parameters (`/foo?genre=mystery`), sometimes called search parameters, not URL parameters (`/foo/genre/mystery`).

I referenced the URL Parameters example in my original question and gave an example of what I meant, so I'm unsure how I could have communicated more clearly. That leads me to think that you are trying to answer my question and I'm not seeing it, but neither of the links you provided seem to show any URLs with a querystring at the end, after a `?`. Please let me know what I'm missing.

Sorry, your question was clear, I don't use search parameters in my app and got confused, I just check it and it's actually `location.search` that return a string `?genre=mystery'. The props passed to the component by `route` are not yet documented, this is what I gathered:

  "path": "/:genre",
  "computedMatch": 
    "path": "/:genre",
    "url": "/foo",
    "isExact": true,
    "params": 
      "genre": "foo"
  "length": 7,
  "action": "POP",
  "location":
    "pathname": "/foo",
    "search": "?genre=mystery",
    "hash": ""
  "match": 
    "path": "/:genre",
    "url": "/foo",
    "isExact": true,
    "params": 
      "genre": "foo"