Simple Tutorials for Smart Learning
When building a dynamic React app, you often need to pass data through URLs — like product IDs, user names, or search filters.
For example:
/users/101 → Display details for user 101
/users/101
/products?id=23 → Show product with ID 23
/products?id=23
In React Router, we handle this using Route Parameters and Query Strings.
Route Parameters allow you to create dynamic URLs that change based on user interaction.They are defined using colon syntax (:) in the route path.
:
Example:/users/:id → :id is a route parameter that can hold dynamic values like 1, 2, 3, etc.
/users/:id
:id
1
2
3
import React from "react"; import { BrowserRouter, Routes, Route, useParams, Link } from "react-router-dom"; function UserProfile() { // Extract the route parameter using useParams const { id } = useParams(); return <h2>👤 Viewing Profile of User ID: {id}</h2>; } function App() { return ( <BrowserRouter> <nav> <Link to="/user/101">User 101</Link> |{" "} <Link to="/user/102">User 102</Link> </nav> <Routes> <Route path="/user/:id" element={<UserProfile />} /> </Routes> </BrowserRouter> ); } export default App;
Explanation:
/user/:id defines a dynamic route.
/user/:id
useParams() extracts the id from the URL.
useParams()
id
Navigating to /user/101 displays User ID: 101.
/user/101
User ID: 101
<Route path="/product/:category/:id" element={<Product />} />
If the URL is /product/electronics/45,then:
/product/electronics/45
const { category, id } = useParams(); console.log(category); // "electronics" console.log(id); // "45"
Useful for eCommerce or dashboard apps.
Query strings appear after a question mark (?) in a URL.They are used for optional parameters, filters, or search terms.
?
Example:
/search?term=react&category=tutorial
To read query strings, we use the useLocation() hook from react-router-dom.
useLocation()
react-router-dom
import React from "react"; import { BrowserRouter, Routes, Route, useLocation, Link } from "react-router-dom"; function SearchPage() { const location = useLocation(); const queryParams = new URLSearchParams(location.search); const term = queryParams.get("term"); const category = queryParams.get("category"); return ( <div> <h2>🔍 Search Page</h2> <p>Search Term: {term}</p> <p>Category: {category}</p> </div> ); } function App() { return ( <BrowserRouter> <nav> <Link to="/search?term=react&category=tutorial">Search React Tutorials</Link> </nav> <Routes> <Route path="/search" element={<SearchPage />} /> </Routes> </BrowserRouter> ); } export default App;
useLocation() gets the current URL object.
URLSearchParams lets you extract query values easily.
URLSearchParams
Works great for search pages, filters, or sorting options.
You can navigate with query parameters using the useNavigate() hook:
useNavigate()
import { useNavigate } from "react-router-dom"; function SearchButton() { const navigate = useNavigate(); const handleSearch = () => { navigate("/search?term=hooks&category=react"); }; return <button onClick={handleSearch}>Search React Hooks</button>; }
This allows you to change routes with parameters dynamically in your app logic.
/search?term=react
/product/45
/products?category=phones
You can use both together for advanced routing.
/user/101?tab=posts
import { useParams, useLocation } from "react-router-dom"; function UserPage() { const { id } = useParams(); const query = new URLSearchParams(useLocation().search); const tab = query.get("tab"); return ( <h3> Viewing User {id}, Tab: {tab} </h3> ); }
Output for /user/101?tab=posts → “Viewing User 101, Tab: posts”
BrowserRouter
HashRouter
useParams
window.location
useLocation