[Solved] React How to get parameter value from query string?
Article
Tarif Hossain
In this article, you can get a quick guide about query string parameters. One of the easiest ways to get query strings with react-router is ‘query-string Package.
Using ‘query-string package get the query string parameter in any component. get parameter in the component render method and all other methods. first, install this package and then follow this example.
Install ‘query-string’ Package
npm install query-string/src/CategoryComponent.js
This is an example URL :
http://localhost:3000/category?id=1&type=cornPasTa
import React from 'react' import queryString from 'query-string'; class CategoryComponent extends React.Component{ render(){ const parsed = queryString.parse(location.search); console.log(parsed); return( <div> <h4>This is Category Component.</h4> </div> ) } } export default CategoryComponent;
Output :
console log 'parsed' variable at line number 8 then we get this output.
{id: "1", type: "cornPasTa"}
we hope it can help you...