How to Get Parameter Value from Query String in React

How to Get Parameter Value from Query String in React?

Query strings are widely used to retrieve information from the URL of pages. Then, it can be accessed for further functioning web application functionalities and is a part of the URL or full query.

It allows sending information using parameters as the key-value pairs. In other words, a query string is a piece of information sent to the server appended to the end of the page URL. A query string is a way to transfer information from one page to another via a URL.

The query string is often attached to the URL with “?”. It is easy to use and does not require any server resources. Almost all browsers support Query String. You do not need to put extra effort into code.

Are you stuck on how to get parameter values from Query String? If yes, then read this blog thoroughly. Or Need technical support in React development? Hire ReactJS developers from Bosc Tech Labs who have enriched experience developing complex applications.

Ways to get parameter value from the query string

Here are the three methods to follow to get parameter values from the query string.

1. You can use a regular expression to get the query string.


import React from "react";
import { useLocation } from "react-router-dom";
const MyComponent = () => {
  const { language } = useLocation();
  const name = new URLSearchParams(language).get("name");


2. To obtain parameter value from query string with React Router

You must use the “useLocation” hook to get parameter value from the query string with React Router. Here is how to do it! Also, check out the guidelines you should follow for best use of React hooks.

import React from "react";
import { useLocation } from "react-router-dom";
constMyComponent = () => {
  const { search } = useLocation();
  const id = new URLSearchParams(search).get("id");

useLocation hook is used to get the URL data properly. Then, you get the query string from the “search” property. Next, it passes in Search to URLSearchParams. So, you will get the value of the “id” query parameter with getting.

URLSearchParams API is built into all the browsers and renders you utility methods for dealing with the query strings. If you create the new instance of URLSearchParams, you need to pass it a query string and then get back the object with the bunch of methods working with that query string.

3. To obtain parameter value from query string without react-router in reactjs app.

If you do not want to use the react-router to get the parameter value from the query string, you can work in react js app. You need to use Here are the steps to follow to get URL parameter values without a react-router in the react js app.

Create react app

Open your terminal and then execute the following common on the terminal to create the new react app.

npx create-react-app my-react-app

You should execute the following command on your terminal to run the React app.

npm start

You can check out your React app on the URL: localhost:3000

Install the bootstrap 4 package and the query string.

To add the query string and bootstrap 4 libraries to your react project, run the command listed below next.

npm install bootstrap --save
npm install query-string

After that, you must add the react-router and boostrap.min.css file in the src/App.js file.

import React from 'react';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
function App() {
  return (
); } export default App;


Create home component

Create the Home.js home component by going to the react js app’s src directory. After that, you must add the following code to it.

import React from 'react'
importqueryString from 'query-string'
class Home extends React.Component{
state = {
    site: 'unknown',
    subject: 'i don't know
  handleQueryString = () => {
     let queries = queryString.parse(
  render() {
    return (
WebSite: {} Subject: {this.state.subject}
); } } export default Home;

The URL http://localhost:3000/?site=tutsmake&subject=get-query-string that can be shown in a react js application is assumed to be in your possession. As a result, when you click the button, your react js app will receive the query string parameter.

Add component in app.js

It’s now time to include the Home.js file in the src/App.js file. The react js component can be added, as shown here.

import React from 'react';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
iimportqueryString from 'query-string'
import Home from './Home'
function App() { 
  return (
); } export default App;

4. Multiple Query parameters


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

export default function EmployeeDetails() {

  const search = useLocation().search;
  const empId = new URLSearchParams(search).get('empId');
  const empName = new URLSearchParams(search).get('empName');

  return (

Employee Details Page

{empId} {empName}
); }


Consequently, you will comprehend the essential techniques for obtaining parameter values from the query string. You can employ any of these techniques. Alternatively, you can hire a react developer because the developer has more knowledge and expertise in this area.

React developers think out of the box when handling the project. So, you will expect the best assistance for your query or project. Remember that you can freely discuss your needs with the developer and get the right solution. They will showcase all possible ways to achieve your requirements using their expertise.