DEV Community

Kazutora Hattori
Kazutora Hattori

Posted on

【React Router】A Summary of useParams

Introduction

Here's a quick summary of useParams.

What is useParams?

“A tool for easily extracting necessary information from URLs.”

For example, consider this URL:

/users/123
Enter fullscreen mode Exit fullscreen mode

Here, the number “123” represents the “user ID”.
But writing code normally won't automatically capture this “123”.

That's where useParams comes in.

const { id } = useParams();
Enter fullscreen mode Exit fullscreen mode

Just writing this lets you extract parts of the URL, like:

id = “123”
Enter fullscreen mode Exit fullscreen mode

A more familiar example?

Product page /products/45 → Want to retrieve “Information for product number 45”

Article page /articles/99 → Want to display “Content for article ID 99”

User page /users/123 → Want to show “Profile for user 123”

For pages like these, where the displayed content changes based on the URL, useParams becomes essential.

Summary

useParams is a convenient mechanism that lets you use parts like :id or :name within a URL in your code.

Top comments (0)