Skip to content
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

[V4] Example for Query Strings needed #4527

Closed
BerndWessels opened this issue Feb 13, 2017 · 1 comment
Closed

[V4] Example for Query Strings needed #4527

BerndWessels opened this issue Feb 13, 2017 · 1 comment

Comments

@BerndWessels
Copy link

Hi

Could you please provide an example how to deal with query strings in V4.

/list?id=5&sort=true

how would a <Route> look like that matches and how to extract the params id and sort?

@pshrmn
Copy link
Contributor

pshrmn commented Feb 13, 2017

The search string is not used in matching locations, only the pathname.

<Route path='/list' component={List} />

You can parse the current location.search string to get the query params as an object. There are a lot of different options for parsing/stringifying query objects. qs is a popular one.

import { parse } from 'qs'

const List = ({ location }) => {
  const query = parse(location.search.substr(1)))
  return (
    <ul>
      <li>ID: {query.id}</li>
      <li>Sort: {query.sort}</li>
    </ul>
  )
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants