reactjsHow do I retrieve query parameters in ReactJS?
In ReactJS, query parameters can be retrieved by using the URLSearchParams API. It is a built-in browser API that provides utility methods to work with query strings.
Example code
// Get the current URL
const url = window.location.href;
// Create an object with the URL's query parameters
const params = new URLSearchParams(url.split('?')[1]);
// Get the value of the `name` query parameter
const name = params.get('name');
Output example
name
Code explanation
window.location.href- The current URLURLSearchParams- A built-in browser API that provides utility methods to work with query stringsurl.split('?')[1]- Splits the URL into two parts, the first part being the path and the second part being the query stringparams.get('name')- Retrieves the value of thenamequery parameter
Helpful links
- URLSearchParams - MDN web docs for URLSearchParams
More of Reactjs
- How can I use ReactJS and ZeroMQ together to create a distributed application?
- How do I zip multiple files using ReactJS?
- How do I create a zip file using ReactJS?
- How do I use Yup validation with ReactJS?
- How do I install Yarn for React.js?
- How can I use Yup with ReactJS?
- How can I use a ReactJS XML editor?
- How can I use ReactJS and Kafka together to develop a software application?
- How do I implement a year picker using ReactJS?
- How can I use zxcvbn in a ReactJS project?
See more codes...