Sign in
Topics
Build Your React App Faster with AI
React Router query params make URL handling simple and effective. Learn how to use URL parameters, query strings, and search params to build scalable React apps with modern tools like Rocket.
Every click on a modern web app often changes the URL, and behind that change lies a powerful mechanism: parameters. Whether it’s searching for a product, filtering results, or sharing a link with a friend, query strings and URL parameters drive the experience.
But if you’ve ever wondered how React apps make sense of those cryptic key-value pairs after a question mark, you’re not alone. For many developers, handling query params in React Router feels like a maze of hooks, objects, and methods.
This blog unravels those complexities. We’ll break down query strings, explain how React Router DOM handles them, and show practical examples of working with search params.
By the end, you’ll not only understand how to get a parameter from a URL in React but also know when to use URL parameters versus query strings in real projects. And we’ll touch on how modern platforms like Rocket.new support these workflows at scale.
React Router is a popular routing library that enables developers to manage navigation, page rendering, and URL routing within React applications. By using React Router, applications can manage complex routes, URL parameters, and even query parameters directly in the browser without page reloads.
Before we examine query parameters, it’s essential to understand that React Router operates by mapping a URL to a component. The router listens for changes in the current location and renders the element accordingly.
Developers can pass parameters, read search parameters, and utilize hooks like useLocation or useSearchParams for working with query strings.
When working with a React app, two primary types of parameters are relevant: URL parameters and query parameters.
URL parameters are dynamic segments in a route, such as /users/:id
, where :id
represents a variable value. On the other hand, query strings (sometimes called query string params) are appended to the end of a URL after a question mark, such as:
1/users?id=101&status=active 2
Both URL parameters and query strings are used to pass values from one component to another, to filter data, or to fetch specific user details. React Router provides multiple methods and hooks to efficiently handle these search parameters.
Query params are key-value pairs added to a URL using a query string. They are widely used to pass data between components without storing it in local storage or relying on global state.
For example, consider the following URL:
1/products?category=electronics&sort=price 2
Here, category and sort act as search params. The app can parse them using React Router and render filtered product data.
React Router v6 introduced a simpler and more reliable way of working with query strings. Instead of relying on additional libraries or manual parsing, developers can use the useSearchParams
hook directly.
The useSearchParams
hook allows components to read and write query parameters as part of the current location object. Unlike older versions of React Router, where developers needed query-string libraries, React Router v6 makes handling search params more straightforward.
Let’s look at a working example that uses the useSearchParams
hook:
1import React from "react"; 2import { useSearchParams } from "react-router-dom"; 3 4function App() { 5 const [searchParams, setSearchParams] = useSearchParams(); 6 const category = searchParams.get("category"); 7 const sort = searchParams.get("sort"); 8 9 return ( 10 <div> 11 <h1>Products</h1> 12 <p>Category: {category}</p> 13 <p>Sort: {sort}</p> 14 <button onClick={() => setSearchParams({ category: "books", sort: "rating" })}> 15 Change Filters 16 </button> 17 </div> 18 ); 19} 20 21export default App; 22
Here:
useSearchParams
returns a URLSearchParams
object that behaves like the window object’s URLSearchParams
.searchParams.get()
method.setSearchParams
.This method is clean, avoids extra parsing code, and is supported by the latest version of React Router.
Ready to streamline your React app’s routing and query parameter handling? Try Rocket.new to build, manage, and deploy modern React projects effortlessly.
In addition to query strings, you may need to access URL parameters defined within routes. This is commonly done with the useParams
hook from react-router-dom
.
Example:
1import React from "react"; 2import { useParams } from "react-router-dom"; 3 4function UserProfile() { 5 const { id } = useParams(); 6 return <h1>User ID: {id}</h1>; 7} 8 9export default UserProfile; 10
In this example:
url /users/42
maps to the route /users/:id
.useParams
hook.ID
.“Exploring React Routing with hands-on practice! From setting up
createBrowserRouter
to building nested routes with<Outlet />
, I’m diving deeper into the real-world workflow of a Full Stack Web Developer.” → Check out the full post here
There are multiple situations where developers need to use query strings inside React apps:
/products?sort=price&order=asc
allows users to sort a product list./login?redirect=/dashboard
./articles?page=3
.By using React Router DOM’s built-in hooks, developers can handle these scenarios without writing a custom serialize function or relying on additional libraries.
While useSearchParams
is recommended in React Router v6, you can still use the useLocation
hook to access the current location object and manually parse the query string.
Example with useLocation
:
1import React from "react"; 2import { useLocation } from "react-router-dom"; 3 4function SearchPage() { 5 const location = useLocation(); 6 const params = new URLSearchParams(location.search); 7 const term = params.get("q"); 8 9 return <h2>Search Term: {term}</h2>; 10} 11 12export default SearchPage; 13
In this method, developers manually handle the new URLSearchParams
object. It provides more flexibility, but also requires writing extra code compared to the useSearchParams
hook.
Sometimes, you need more than two values or arrays inside query strings. In such cases, query parameters can look like this:
1/products?category=books&tags=fiction,thriller&price=low 2
To handle arrays, developers can split a string using commas or multiple key-value pairs. A custom hook can simplify this process and make code reusable.
Example custom hook:
1import { useSearchParams } from "react-router-dom"; 2 3export function useQueryParam(key) { 4 const [searchParams, setSearchParams] = useSearchParams(); 5 const paramValue = searchParams.get(key); 6 return [paramValue, (value) => setSearchParams({ [key]: value })]; 7} 8
This hook extracts a single search param and provides a setter function, reducing repetition across components.
When working with server-side rendering in React, managing query strings becomes more complex because both the server and client need to interpret them. React Router supports SSR workflows, but developers must carefully synchronize search parameters across both sides.
Many enterprise-grade app development platforms now offer support for server-side rendering with built-in routing. Tools like Rocket.new  help teams ship applications faster while handling URL parameters and query parameters in a structured way. Rocket integrates seamlessly with modern workflows, emphasizing scalability to make it easier to manage search parameters, query strings, and routing logic without incurring extra overhead.
Other platforms that focus on enterprise-grade security and modern workflow support include DhiWise, Vercel, Netlify, Supabase, and Firebase. Each provides routing, deployment, and integration capabilities that complement React Router projects.
Here’s a practical example of a React component that reads multiple search params and updates them dynamically:
1import React from "react"; 2import { useSearchParams } from "react-router-dom"; 3 4function FilterPage() { 5 const [searchParams, setSearchParams] = useSearchParams(); 6 const category = searchParams.get("category"); 7 const price = searchParams.get("price"); 8 9 const handleFilterChange = () => { 10 setSearchParams({ category: "clothing", price: "high" }); 11 }; 12 13 return ( 14 <div> 15 <h2>Filters</h2> 16 <p>Category: {category}</p> 17 <p>Price: {price}</p> 18 <button onClick={handleFilterChange}>Change Filters</button> 19 </div> 20 ); 21} 22 23export default FilterPage; 24
In this example:
While both approaches pass data through the URL, query strings are better suited for optional values, filters, and searches. URL parameters are more suitable when the data is mandatory, such as fetching a specific user profile by ID.
If the following URL is required:
1/users/123 2
It makes sense to define it as a URL parameter. If the URL should look like this:
1/users?id=123&tab=settings 2
Then, the query params are more appropriate.
In the previous example, we used useSearchParams
for handling two values at once. In the last example, developers can extend this approach to pass entire objects, arrays, or even search objects representing multiple filters.
React applications benefit from structuring query strings in a manner that aligns with the user experience. Other users accessing the same link can replicate the state instantly, making query strings a powerful tool for collaboration, analytics, and navigation.
Working with query strings, URL parameters, and search parameters in React Router may seem complex initially, but React Router v6 provides powerful hooks, such as useSearchParams, to simplify the process.
From reading key-value pairs to managing multiple parameters and integrating them into enterprise-grade platforms like Rocket.new , developers can build scalable apps that effectively handle changes to the URLSearchParams object.
By mastering these techniques, React developers can create cleaner navigation flows, pass values seamlessly, and manage application state directly inside the URL.