Skip to main content
St Louis

Back to all posts

How to Get Query Params From Url In React.js?

Published on
5 min read
How to Get Query Params From Url In React.js? image

Best React.js Query Tips to Buy in October 2025

1 Learn React with TypeScript: A beginner's guide to building real-world, production-ready web apps with React 19 and TypeScript

Learn React with TypeScript: A beginner's guide to building real-world, production-ready web apps with React 19 and TypeScript

BUY & SAVE
$40.49 $49.99
Save 19%
Learn React with TypeScript: A beginner's guide to building real-world, production-ready web apps with React 19 and TypeScript
2 The Road to React: Your journey to master plain yet pragmatic React.js

The Road to React: Your journey to master plain yet pragmatic React.js

BUY & SAVE
$29.99
The Road to React: Your journey to master plain yet pragmatic React.js
3 Modern Full-Stack React Projects: Build, maintain, and deploy modern web apps using MongoDB, Express, React, and Node.js

Modern Full-Stack React Projects: Build, maintain, and deploy modern web apps using MongoDB, Express, React, and Node.js

BUY & SAVE
$20.60 $44.99
Save 54%
Modern Full-Stack React Projects: Build, maintain, and deploy modern web apps using MongoDB, Express, React, and Node.js
4 React Key Concepts: An in-depth guide to React's core features

React Key Concepts: An in-depth guide to React's core features

BUY & SAVE
$23.71 $49.99
Save 53%
React Key Concepts: An in-depth guide to React's core features
5 React.Js: React.Js Programming, In 8 Hours, For Beginners, Quick Start Guide: React.Js Library Crash Course Tutorial & Exercises

React.Js: React.Js Programming, In 8 Hours, For Beginners, Quick Start Guide: React.Js Library Crash Course Tutorial & Exercises

BUY & SAVE
$13.99
React.Js: React.Js Programming, In 8 Hours, For Beginners, Quick Start Guide: React.Js Library Crash Course Tutorial & Exercises
6 Mastering Node.js Web Development: Go on a comprehensive journey from the fundamentals to advanced web development with Node.js

Mastering Node.js Web Development: Go on a comprehensive journey from the fundamentals to advanced web development with Node.js

BUY & SAVE
$29.69 $49.99
Save 41%
Mastering Node.js Web Development: Go on a comprehensive journey from the fundamentals to advanced web development with Node.js
7 React JS: A Step-by-Step Guide to Mastering the Top Web Development Library from Basic to Advanced Level (Coding Made Easy)

React JS: A Step-by-Step Guide to Mastering the Top Web Development Library from Basic to Advanced Level (Coding Made Easy)

BUY & SAVE
$14.95
React JS: A Step-by-Step Guide to Mastering the Top Web Development Library from Basic to Advanced Level (Coding Made Easy)
8 React: The Comprehensive Guide to Mastering React.js with Hands-on Examples, Expert Tips, and Everything You Need to Build Dynamic, Scalable User Interfaces (Rheinwerk Computing)

React: The Comprehensive Guide to Mastering React.js with Hands-on Examples, Expert Tips, and Everything You Need to Build Dynamic, Scalable User Interfaces (Rheinwerk Computing)

BUY & SAVE
$41.31 $59.95
Save 31%
React: The Comprehensive Guide to Mastering React.js with Hands-on Examples, Expert Tips, and Everything You Need to Build Dynamic, Scalable User Interfaces (Rheinwerk Computing)
9 React and React Native: A complete hands-on guide to modern web and mobile development with React.js, 3rd Edition

React and React Native: A complete hands-on guide to modern web and mobile development with React.js, 3rd Edition

BUY & SAVE
$59.16 $83.99
Save 30%
React and React Native: A complete hands-on guide to modern web and mobile development with React.js, 3rd Edition
+
ONE MORE?

To get query params from a URL in React.js, you can use the URLSearchParams API. First, you need to get the current URL using window.location.search, which returns the query string portion of the URL. Then, you can create a new URLSearchParams object by passing in the query string as a parameter. Finally, you can use the get() method of the URLSearchParams object to retrieve specific query parameters by their names. Remember to handle any potential errors that may occur during this process.

What is the limitation of using localStorage for storing query params in React.js?

One limitation of using localStorage for storing query params in React.js is that the data stored in localStorage is persisted across sessions and can potentially be accessed by other scripts running in the same domain. This can pose a security risk as sensitive information may be exposed.

Another limitation is that localStorage has a limited capacity (typically 5MB per domain) so storing large amounts of data may lead to performance issues.

Additionally, using localStorage for query params may not be suitable for cases where the data needs to be passed between different components or shared between different users, as localStorage is specific to the browser session of the current user. In such cases, a more scalable and secure solution such as using a global state management library like Redux or passing props between components may be more appropriate.

How to extract query params using React Router in React.js?

You can extract query parameters using React Router by accessing the location object passed as a prop to your component by the router. Here's an example of how you can extract query parameters using React Router in a functional component:

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

const MyComponent = () => { const location = useLocation(); const queryParams = new URLSearchParams(location.search);

const param1 = queryParams.get('param1'); const param2 = queryParams.get('param2');

return ( Query Params: Param 1: {param1} Param 2: {param2} ); };

export default MyComponent;

In this example, we first import the useLocation hook from react-router-dom to access the current location object. We then use the URLSearchParams constructor to parse the query parameters from location.search. Finally, we can retrieve individual query parameters using the get method on the URLSearchParams object.

Remember to wrap your component with a Route component in your App.js file to ensure that the location object is passed down as a prop:

import React from 'react'; import { BrowserRouter as Router, Route } from 'react-router-dom'; import MyComponent from './MyComponent';

const App = () => { return ( ); };

export default App;

With this setup, you can extract and display query parameters in your React components using React Router.

How do you access query parameters in React.js?

In React.js, you can access query parameters from the URL using the react-router-dom package. You can use the useLocation hook from react-router-dom to get access to the location object, which contains the search property that includes query parameters.

Here is an example of how you can access query parameters in React.js:

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

function MyComponent() { const location = useLocation(); const searchParams = new URLSearchParams(location.search);

// Get the value of a specific query parameter const paramValue = searchParams.get('paramName');

return ( Query parameter value: {paramValue} ); }

export default MyComponent;

In this example, we first import the useLocation hook from react-router-dom. We then use the hook to get the location object, which contains the search property that includes query parameters.

We create a new URLSearchParams object with the search property to access the query parameters. We can then use the get method on the searchParams object to get the value of a specific query parameter by providing the parameter name as an argument.

Finally, we can use the query parameter value in our component as needed.

How to extract query params from URL in React.js?

One way to extract query params from a URL in React.js is by using the URLSearchParams API. Here's an example code snippet to extract query params from a URL in a React component:

import React from 'react';

const MyComponent = () => { const urlParams = new URLSearchParams(window.location.search);

// Get specific query param const param1 = urlParams.get('param1'); const param2 = urlParams.get('param2');

return ( Param 1: {param1} Param 2: {param2} ); }

export default MyComponent;

In this example, URLSearchParams is used to extract query params from the current URL's search parameters. The get method is then used to retrieve specific query parameters by their names.

Remember to make sure that the URLSearchParams API is supported by the browser you are targeting.

What is the alternative method to retrieve query params in React.js?

One alternative method to retrieve query params in React.js is by using the useLocation hook from the react-router-dom library. The useLocation hook provides the current location object which includes information about the URL, including query parameters.

Here's an example of how to use the useLocation hook to retrieve query params in React.js:

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

const MyComponent = () => { const location = useLocation(); const queryParams = new URLSearchParams(location.search);

const param1 = queryParams.get('param1'); const param2 = queryParams.get('param2');

return ( Param 1: {param1} Param 2: {param2} ); };

In this example, we first import the useLocation hook from react-router-dom. We then use the useLocation hook to retrieve the current location object. We create a new URLSearchParams object from the search property of the location object, which contains the query parameters. We can then use the get method of the URLSearchParams object to retrieve individual query parameters by their names.