Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the useLocation hook from react-router-dom to retrieve the query string. Then, you can use the qs library to parse the query string into an object and retrieve the value of the desired parameter.

Here is an example code snippet:

import { useLocation } from 'react-router-dom';
import qs from 'qs';

function MyComponent() {
  const location = useLocation();
  const queryParams = qs.parse(location.search, { ignoreQueryPrefix: true });
  const myParamValue = queryParams.myParam;

  // use myParamValue in your component logic
}

In this example, we first retrieve the location object using the useLocation hook. We then use the qs.parse method to parse the query string (ignoring the leading ? character) into an object. Finally, we access the value of the desired parameter (myParam) from the queryParams object.