# Next.js Get URL Params

> In Next.js 13, some hooks and props let you access the URL params depending if your component is a client or server component.

**URL:** https://sentry.io/answers/get-url-params-next-js-13/

---

## The Problem

In Next.js 13, you may need to access the URL parameters at a particular route and use those parameters in a child component.

## The Solution

Before Next.js 13, this was done through the `useRouter()` and `router.query` props. However, in Next.js 13 some hooks and props enable you to access the URL parameters depending on whether your component is a client or server component.

There are two different types of URL parameters we might want to access:

- Query Strings.
- [Dynamic Params](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes).

Each of these has a hook that lets you access it.

### Query Strings

In a server component, you can access the query string by using the `searchParams` prop. In Next.js 15+, `searchParams` is a Promise that must be awaited:

```jsx
export default async function Dashboard(props) {
  const qs = await props.searchParams;

  return (
    <main>
      <div>
        <h1>Dashboard</h1>
        <p>Search Params are {qs.myParam}</p>
      </div>
    </main>
  );
}
```

The resolved `searchParams` value is an object that includes the parameters as top-level fields. In this scenario, we access the value of `myParam`.

If we navigate to `/dashboard?myParam=”Hello World”` the rendered value will be “Hello World”.

In a client component, we can use the `useSearchParams()` hook:

```jsx
"use client";

import { useSearchParams } from "next/navigation";

export default function Dashboard() {
  const searchParams = useSearchParams();
  const myParam = searchParams.get("myParam");

  return (
    <main>
      <div>
        <h1>Dashboard</h1>
        <p>Search Params are {myParam}</p>
      </div>
    </main>
  );
}
```

### Dynamic Params

In this scenario, we want to access the URL parameter that is part of a dynamic route. Suppose we have the following route definition:

```
app/
dashboard/
├─ [dynamic]/
│  ├─ page.js
```

Similarly to search parameters, if we want to access the dynamic parameters in a server component, we can use the `params` prop. In Next.js 15+, `params` is also a Promise:

```jsx
export default async function DynamicRouteParam(props) {
  const dynamicParams = await props.params;

  return (
    <main>
      <div>
        <h1>Dashboard</h1>
        <p>Params are {dynamicParams.dynamic}</p>
      </div>
    </main>
  );
}
```

If we navigate to `/dashboard/my-param`, the value “my-param” would be rendered.

To achieve this for a client component, use the `useParams()` hook:

```jsx
"use client";

import { useParams } from "next/navigation";

export default function DynamicClientRouteParam() {
  const params = useParams();
  const dynamicParams = params.dynamic;
  return (
    <main>
      <div>
        <h1>Dashboard</h1>
        <p>Params are {dynamicParams}</p>
      </div>
    </main>
  );
}
```

## Additional Reading

You can read more about routing and the API functions related to URL parameters in the official Next.js documentation and API reference:

  - [`useSearchParams`](https://nextjs.org/docs/app/api-reference/functions/use-search-params)
  - [`useParams`](https://nextjs.org/docs/app/api-reference/functions/use-params)
  - [Routing](https://nextjs.org/docs/app/building-your-application/routing)

---

*Source: [sentry.io/answers/get-url-params-next-js-13/](https://sentry.io/answers/get-url-params-next-js-13/)*
