How to Get Parameter Value from Query String in ReactJS? Last Updated : 03 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.ApproachTo 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 AppStep 1: Create a React application using the following command:npx create-react-app foldernameStep 2: After creating your project folder i.e. folder name, move to it using the following command:cd foldernameStep 3: Install the required modulenpm install query-string Project Structure:It will look like the followingThe 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 startOutput: Comment More infoAdvertise with us K KapilChhipa Follow Improve Article Tags : ReactJS Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Like