Skip to content
the poor coder

get search params from url react router 5

Poor Man
Published
Reading time
2 min read
On this page

How to Get Search Params from URL using React Router 5#

Have you ever needed to retrieve search parameters from the URL in your React application? React Router 5 provides an easy way to do so using the useLocation hook from the react-router-dom package.

Step 1: Import the necessary components#

First, make sure you have both react-router-dom and highlight.js installed in your project:

Bash
npm install react-router-dom highlight.js

Then, import the components you'll need in your React component:

JavaScript
import React from 'react';
import { useLocation } from 'react-router-dom';
import hljs from 'highlight.js';

Step 2: Retrieve the search parameters#

Next, use the useLocation hook to retrieve the search parameters from the URL:

JavaScript
function MyComponent() {
  const location = useLocation();
  const params = new URLSearchParams(location.search);
  
  // do something with the params...
  
  return (
    <div>
      <p>Search parameters: {params.toString()}</p>
    </div>
  );
}

export default MyComponent;

The location.search property contains the search parameters as a string, which we can pass to the URLSearchParams constructor to create a new object we can work with.

Bonus: Using Multiple Search Parameters#

If you have multiple search parameters in the URL, you can use the get() method of the URLSearchParams object to retrieve a specific parameter:

JavaScript
function MyComponent() {
  const location = useLocation();
  const params = new URLSearchParams(location.search);
  
  const searchTerm = params.get('q');
  const pageNumber = params.get('page');
  
  // do something with the search term and page number...
  
  return (
    <div>
      <p>Search term: {searchTerm}</p>
      <p>Page number: {pageNumber}</p>
    </div>
  );
}

export default MyComponent;

In this example, we're retrieving two search parameters - q and page - and using their values in our component.

Conclusion#

Retrieving search parameters from the URL in your React application using React Router 5 is simple and straightforward. By using the useLocation hook and the URLSearchParams object, you can easily access and use this data in your components.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC