Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get query parameters in reactjs

I am using versions

"react": "^0.13.3",
"react-router": "^0.13.3" 

i want to receive the params in which it navigate

1)Navigation done properly

2)what when i try to use this.props.query.id for receiving arguments it show undefined.

like image 448
IshanGarg Avatar asked Apr 05 '17 13:04

IshanGarg


People also ask

How do I get parameters in React router?

To get path params in React Router, we can use the useParams hook. We create the Child component that calls the useParams hook to return an object with the route params in the URL. And we render the value of the id param on the page. In App , we have 2 links that goes to routes with different id param values.


2 Answers

upgrade to react-router v4 and ;

Use this instead:

this.props.location.search

if you have a URL like mysite.com/user/tweets?filter=top&origin=im

Doing:

console.log(this.props.location.search); //yields "?filter=top&origin=im"

Thus to get the value of "filter" or "origin", you need to use a package for parsing query strings because react-router doesn’t come with built-in support for parsing query strings. E.g. using 'query-string' library on NPM you can do:

import queryString from 'query-string';

const UrlQueryStrings = this.props.location.search;
const queryValues = queryString(UrlQueryStrings);

console.log(queryValues.filter); // Gives "top"
console.log(queryValues.origin); // Gives "im"
like image 66
daymorelah Avatar answered Oct 14 '22 21:10

daymorelah


Use this:

this.props.location.query.id
like image 25
Mayank Shukla Avatar answered Oct 14 '22 22:10

Mayank Shukla