How to get query string using React?

Within your component, you can do something like this:

const values = queryString.parse(this.props.location.search)
console.log(values.filter) 
console.log(values.origin) 

There is more here.


React doesn't handle URL search parameters. You need to look in the window object for them instead. Here's how you would get the value of query:

let search = window.location.search;
let params = new URLSearchParams(search);
let foo = params.get('query');

I created a separate function for the query string.

function useQuery() {
  return new URLSearchParams(window.location.search);
}

Then I can provide the query string I want to get. In your case its query

  let query = useQuery().get('query');

Tags:

Reactjs