Shivan M.
—In Next.js 13, you may need to access the URL parameters at a particular route and use those parameters in a child component.
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:
Each of these has a hook that lets you access it.
In a server component, you can access the query string by using the searchParams
prop:
export default function Dashboard(props) { const qs = props.searchParams; return ( <main> <div> <h1>Dashboard</h1> <p>Search Params are {qs.myParam}</p> </div> </main> ); }
The searchParams
prop 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:
"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> ); }
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:
export default function DynamicRouteParam(props) { const dynamicParams = 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:
"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> ); }
You can read more about routing and the API functions related to URL parameters in the official Next.js documentation and API reference:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.