Skip to content
tezvyn:
Menu

useParams: Accessing Dynamic URL Segments

Source: nextjs.orgMediumHow cards are made

The useParams hook lets client components read dynamic URL segments. On a route like /posts/[slug], it returns an object like { slug: 'my-post' }. Remember, it's for client components only and won't see query strings.

Why it exists

Web apps need to display content based on the URL. For dynamic pages, like a user profile or a product detail page, a unique identifier is often part of the URL path itself. We need a way to access this identifier from within our components to fetch and display the correct data.

The mental model

Think of useParams as a client-side listener for the URL's "wildcard" sections. You define placeholders in your folder structure (e.g., app/posts/[slug]/page.js), and useParams lets your component code read whatever value fills that placeholder in the user's browser.

How it works

In a Next.js App Router project, you first define a dynamic segment by naming a folder with brackets, like [id]. Inside a client component (marked with "use client"), you import useParams from next/navigation. Calling const params = useParams() returns an object where keys correspond to your dynamic segment names and values are the actual segments from the current URL. For a URL like /users/42, params would be { id: '42' }. The values are always strings.

When to use it

Use useParams inside any Client Component that needs to know the dynamic parts of the current URL path. This is common for fetching data on the client side based on an ID, setting active states in navigation, or rendering titles and headers that include the dynamic segment.

When not to use it

The primary footgun is trying to use it in a Server Component. Server Components don't use hooks for this; they receive params directly as page props (e.g., function Page({ params })). Also, do not use useParams to get query string parameters (the part after ?). For that, use the useSearchParams hook instead.

One canonical example

To build a user profile page at /profile/[username], you would create a file at app/profile/[username]/page.jsx. The component would look like this: 'use client';

import { useParams } from 'next/navigation';
export default function ProfilePage() {
const params = useParams();

return <h1>Profile for {params.username}</h1>; } If the URL is /profile/jane-doe, this component renders a heading with "Profile for jane-doe".

Interview question

A Next.js client component needs to display content based on a dynamic identifier in the URL path, like /products/123. Which is the correct way to access '123'?

  • a.Read the identifier directly from window.location.pathname within the component.
  • b.Import useParams from next/navigation and access the identifier from its returned object.Correct
  • c.Define a Server Component and receive the identifier as a prop.
  • d.Use useSearchParams to extract the identifier from the URL.
Why?

The useParams hook is specifically for client components to read dynamic URL segments from the path. Server Components receive these parameters as props, not through a hook, making option C incorrect for a client component.

Just read this? Test yourself on what you have been reading.

Read the original → nextjs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles