Provide an example of using the useParams hook in React Router.
Last Updated :
28 Apr, 2024
React Router is a popular library for routing in React applications. One of its powerful features is the useParams
hook, which allows developers to access parameters from the URL in functional components. In this article, we'll explore the useParams
hook in React Router with a practical example.
Prerequisites:
What is useParams hook?
The useParams
hook is a part of React Router's hooks API. It provides a simple way to access URL parameters in functional components without the need for class components or prop drilling.
Approach
- Import the useParams hook from the 'react-router-dom' library at the beginning of your file where you'll be using it. You'll typically need to do this in the file where you define the component that will utilize the URL parameters.
- Within your functional component, call the useParams hook. This hook returns an object containing all the parameters from the URL.
- If your URL has named parameters (e.g., '/users/:id'), you can access them directly from the object returned by useParams. Use dot notation or bracket notation to access the specific parameter you need.
- Once you have accessed the parameter value, you can use it in your component logic or JSX as needed.
Steps to create the application
Step 1: Create a React application by using the following command in the terminal.
npx create-react-app myapp
Step 2: Now, go to the project folder i.e. myapp by running the following command.
cd mypp
Step 3:Â Install the necessary packages/libraries in your project using the following commands.
npm i react-router-dom
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.16.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: In this example we have a user profile page that displays userId. We'll employ the useParams hook to extract the user's ID from the URL and display it on the profile page.
JavaScript
// App.jsx
import React from 'react';
import {
BrowserRouter as Router,
Routes, Route,
Link
} from 'react-router-dom';
import UserProfile from './UserProfile';
const App = () => {
return (
<Router>
<div>
<nav style={styles.nav}>
<ul style={styles.navList}>
<li style={styles.navItem}>
<Link to="/users/123">
User 123
</Link>
</li>
<li style={styles.navItem}>
<Link to="/users/456">
User 456
</Link>
</li>
<li style={styles.navItem}>
<Link to="/users/789">
User 789
</Link>
</li>
</ul>
</nav>
<Routes>
<Route path="/users/:id"
element={<UserProfile />} />
</Routes>
</div>
</Router>
);
};
const styles = {
nav: {
backgroundColor: '#f4f4f4',
padding: '10px 0',
},
navList: {
listStyleType: 'none',
margin: 0,
padding: 0,
textAlign: 'center',
},
navItem: {
display: 'inline-block',
margin: '0 10px',
},
};
export default App;
JavaScript
// UserProfile.jsx
import React from 'react'
import { useParams } from 'react-router-dom'
const UserProfile = () => {
const { id } = useParams()
return (
<div style={styles.container}>
<h2>User Profile</h2>
<p>User ID: {id}</p>
</div>
)
}
const styles = {
container: {
padding: '20px',
border: '1px solid #ccc',
borderRadius: '5px',
margin: '20px auto',
width: '300px',
textAlign: 'center'
}
}
export default UserProfile
Output: In UserProfile.jsx file, This component extracts the id
parameter from the URL using the useParams
hook and displays it in the user profile page and App.jsx file is the main component where React Router is configured. It sets up navigation links
(Link
components) to different user profiles and defines a route (Route
component) that renders the UserProfile
component when the URL matches the pattern /users/:id
.

Conclusion
In this article, we explored the useParams
hook in React Router by building a simple user profile page example. We learned how to access URL parameters in functional components and use them to render dynamic content based on the URL. The useParams
hook simplifies URL parameter handling in React applications, making it easier to build dynamic and interactive web pages.
Similar Reads
Explain the purpose of the Link component in React Router.
React Router is a library in the React JS application. It is used to navigate the page to another page. It allows developers to create a seamless and dynamic user experience. It has many features the 'Link' Component stands out as a powerful tool for creating navigation links in React Applications.
3 min read
Provide an Example Scenario where Portals are Useful in React
React Portals offer a powerful solution for rendering components outside the typical DOM hierarchy, without disrupting the natural flow of events within the React component tree. This capability is particularly beneficial for creating UI elements like modals, tooltips, and notification popups, which
2 min read
What are the React Router hooks in React v5?
React Router hooks perform client-side single-page routing that provides a functional and streamlined approach to managing navigation in React applications. It provides a way to directly access the state of the router from functional components, this simplifies tasks like retrieving information abou
5 min read
How to use the useHistory hook in React Router?
React Router is a standard library system built on top of React and used to create routing in the React application using the React Router Package. It enables the navigation between views of various components in a React Application. React Router is useful for Single Page Applications (SPAs). To use
4 min read
Purpose of useReducer hook in React
The useReducer hook is a state management hook in React that provides an alternative to the useState hook. It is used when the state of a component is complex and requires more than one state variable. Syntax:const [state, dispatch] = useReducer(reducer, initialState, init)reducer: The reducer is a
3 min read
What are the benefits of using hooks in React-Redux?
Have you ever wondered how users create amazing websites and apps? Well, in the world of programming, they have some cool tools, and today we're going to explore one of them called "Hooks" in the superhero team of React-Redux. Prerequisites:ReactReact-ReduxReact HooksJavaScriptWhat are Hooks?Hooks a
2 min read
Give an example of using useDebugValue to inspect custom hooks?
useDebugValue is a React hook that allows you to display a label for custom hooks in React DevTools. This can be useful for debugging and inspecting custom hooks. Key Features:Debugging Made Easier: useDebugValue is like a magnifying glass for custom hooks in React. It helps users see inside these h
3 min read
useRoutes Hook in React Router
React Router is a library that is used for handling navigation and routing in React applications. It provides a way to navigate between different components or pages while maintaining a single-page application (SPA) structure. One of the key features is the useRoutes hook. It allows you to define ro
4 min read
How to set up the Provider Component in a React application ?
In React applications, the Provider component is often used in conjunction with context to provide data to components deep down in the component tree without having to explicitly pass props through every level of the tree. Prerequisites:JavaScriptReactNode & NPMSteps to Create React Application:Step
3 min read
How to handle Nested Routes in React Router ?
React Router allows us to create a hierarchical structure where child components can be rendered within parent components, resulting in a seamless navigation flow. In this article, we will see how to handle nested Routes in React Router. Approach to handle Nested RoutesTo implement nested routes in
3 min read