How to Get Parameter Value from Query String in ReactJS?
Last Updated :
03 Oct, 2024
In React query parameter are a way to pass the data as props from the url to react components. Getting parameter value from query string helps to render the dynamic data depending on the query.
Approach
To get the parameter value from query string in React we will be using the query-string packge. We will get the url using window.location and parse the string with the help of queryString.parse method.
For example, the query string is ?site=gfg&subject=react then after parsing object will be:
{
site:"gfg",
subject:"react"
}
Steps to Create React App
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. folder name, move to it using the following command:
cd foldername
Step 3: Install the required module
npm install query-string
Project Structure:
It will look like the following

The updated dependencies in the package.json file are:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"query-string": "^9.1.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Example: This example access the query parameters by parsing the URL's query string using queryString.parse() and updating the component's state with the retrieved values.
JavaScript
// Filename: App.js
import React, { useState } from "react";
import queryString from "query-string";
const App = () => {
// Using useState hook for managing state
const [site, setSite] = useState("unknown");
const [subject, setSubject] = useState("i dont know");
const handleQueryString = () => {
// Parsing the query string from the window.location.search
const queries = queryString.parse(window.location.search);
console.log(queries);
setSite(queries.site || "unknown");
setSubject(queries.subject || "i dont know");
};
return (
<div style={{ margin: 200 }}>
<p> WebSite: {site} </p>
<p> Subject: {subject} </p>
<button onClick={handleQueryString} className="btn btn-primary">
click me
</button>
</div>
);
};
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
Similar Reads
How to Get Query Parameters from a URL in VueJS ? Query parameters are part of a URL that assigns values to specified parameters. They start after the question mark and are separated by ampersands ("&") in the URL. For example, in the URL https://example.com?name=John&age=23, name=John and age=23 are query parameters. The below-listed metho
8 min read
How to Get Query String Parameter in SvelteKit? In this article, we will explore how to retrieve query string parameters in SvelteKit applications. Query parameters are often used to pass data in URLs, such as user preferences or search queries. Understanding how to access these parameters is crucial for building dynamic web applications.These ar
3 min read
How to Get Query Parameters from URL in Next.js? In Next.js, getting query parameters from the URL involves extracting key-value pairs from the URL string to access specific data or parameters associated with a web page or application, aiding in dynamic content generation and customization.To get query parameters from the URL in next.js we have mu
5 min read
How to retrieve GET parameters from JavaScript ? In order to know the parameters, those are passed by the âGETâ method, like sometimes we pass the Email-id, Password, and other details. For that purpose, We are using the following snippet of code. When you visit any website, ever thought about what the question mark '?' is doing in the address bar
2 min read
How To Get Multiple Checkbox Values In ReactJS? Handling checkboxes in ReactJS becomes important when creating forms or managing user input. If you need to select multiple options, we can do it by implementing multiple checkboxes. In this article, we'll explore how to get the values of multiple checkboxes in ReactJS, manage the state, and display
4 min read
How to Get query params using Server component in Next.js ? NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. We will discuss different approaches to get query params using the server comp
3 min read