by Sai gowtham

How to use the useParams hook in React router

In this tutorial, we are going to learn about how to use the useParams() hook in react router.

If you are new to hooks then check out my react hooks introduction tutorial.

useParams() hook

The useParams() hook helps us to access the URL parameters from a current route.

Example:

Consider, we have a route like this in our react app.

<Route path="/users/:id" component={Users} />

Now, we can access the :id param value inside a Users component by using the useParams() hook.

Users.js
import React from "react";
import { useParams } from "react-router-dom";
export default function Users() {
  const { id } = useParams();
  return (
    <div>
      <h1>User id is {id}</h1>    </div>
  );
}

In the above code, we first imported the useParams() hook from the react-router-dom package.

Inside the Users function, we invoked a useParams() hook that returns an object with key/value pairs where the key is id and value is whatever we passed after /users/ route in the browser.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course