In ReactJS, when building single-page applications (SPAs) with dynamic routing, you often need to access dynamic data from the URL.
For example, in a blog application, the URL will change depending on the post being viewed, like /post/:id. The useParams hook, provided by the react-router-dom package, allows you to access these dynamic parts of the URL, known as route parameters, in your components.
What is useParams?
useParams is a hook in React Router that allows you to access route parameters from the current URL. A route parameter is a part of the URL that is variable (e.g., /product/:id), and it can be used to pass data to components dynamically.
const { param1, param2, ... } = useParams();
- param1, param2, etc., are the names of the route parameters defined in the path.
- useParams returns an object where the keys are the parameter names, and the values are the corresponding values from the URL.
Prerequisites:
Steps to Create React Application and Installing Modules
Step 1: Create a React application using the following command.
npx create-react-app useparams_react
Step 2: After creating your project folder i.e. useparams_react, move to it using the following command.
cd useparams_react
Step 3: After creating the ReactJS application, Install the react-router-dom and react-dom packages using the following command.
npm install react-router-dom@6
Project structure:

The updated dependencies in package.json file.
"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.9.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
React Code:
JavaScript
import React from "react";
import { BrowserRouter as Router, Route, Routes, useParams } from "react-router-dom";
function BlogPost() {
let { id } = useParams();
return <div style={{ fontSize: "50px" }}>Now showing post {id}</div>;
}
function Home() {
return <h3>Home page</h3>;
}
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/page/:id" element={<BlogPost />} />
</Routes>
</Router>
);
}
export default App;
- useParams is called inside the BlogPost component to fetch the dynamic parameter id from the URL.
- The URL /post/:id means that the value for id is dynamically passed and can be accessed via useParams.
Output
Similar Reads
ReactJS useId Hook React useId Hook is introduced for the ReactJS versions above 18. This hook generates unique IDs i.e, returns a string that is stable across both the server and the client sides. Prerequisite: Introduction and installation of ReactJSReact Hooks Syntax: const id = useId() Creating React Application:
3 min read
ReactJS useSelect hook The useSelect is a custom hook provided by the Rooks package for React. It is a list selection hook that helps select values from a list. Arguments: list: It is of the type array which describes the list of items for the selection. The default value is undefined.initialIndex -It is of the type numbe
2 min read
ReactJS useEffect Hook The useEffect hook is one of the most commonly used hooks in ReactJS used to handle side effects in functional components. Before hooks, these kinds of tasks were only possible in class components through lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount.What is
4 min read
React useState Hook The useState hook is a function that allows you to add state to a functional component. It is an alternative to the useReducer hook that is preferred when we require the basic update. useState Hooks are used to add the state variables in the components. For using the useState hook we have to import
5 min read
ReactJs useDebugValue Hook React useDebugValue Hook is introduced for the ReactJs versions above 18. React useDebugValue Hook helps developers debug custom hooks in React Developer Tools by adding additional information and labels to those hooks. Prerequisite:ReactJSReact Developer ToolsReact HooksReact Custom HooksApproach:T
2 min read
React-Router Hooks React-Router is a popular React library that is heavily used for client-side routing and offers single-page routing. It provides various Component APIs( like Route, Link, Switch, etc.) that you can use in your React application to render different components based on the URL pathnames on a single pa
11 min read
React Hooks Tutorial React Hooks were introduced to solve some problems with class components in React. With Hooks, you can now add state, lifecycle methods, and other React features to functional components, which previously only class components could do. This makes development simpler because you can handle stateful
3 min read
ReactJS Hooks Reference React hooks are functions that allow you to use state and other React features in functional components. Hooks were introduced in React 16.8, enabling developers to manage state and lifecycle features without needing class components. They simplify the development process and make it easier to write
3 min read
State Hooks in React State Hooks, introduced in React 16.8, revolutionized how developers manage state in functional components. Before State Hooks, state management was primarily confined to class components using the setState method. State Hooks, such as useState, enable functional components to manage local state eff
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