Answers to Common Next.js Questions
Tags: nextjs, react, webdev, javascript
Published: 2024-05-05 14:25:46

Web development went from server rendered applications to single page applications rendered fully on the client, and then it all got complicated. Next.js have now cleared away the old perspective of client and servers and provided us with a high performance framework which is a big black box containing both client and server. But don't worry, I'll here provide a FAQ with common questions about Next.js and its Server Components.

![Frontend devs got tricked](https://www.perssondennis.com/images/articles/answers-to-common-nextjs-questions/frontend-devs-got-tricked.webp)
_At least we get to use JavaScript for the server code_

## In This Article

1. [How To Read URL Params in a Client Component?](#1-how-to-read-url-params-in-a-client-component)
1. [How To Read URL Params in a Page File?](#2-how-to-read-url-params-in-a-page-file)
1. [How To Read URL Params in a Next.js Layout File?](#3-how-to-read-url-params-in-a-nextjs-layout-file)
1. [How To Read URL Params in a Server Component?](#4-how-to-read-url-params-in-a-server-component)
1. [Why Doesn't searchParams Exist in Next.js Layout Files?](#5-why-doesnt-searchparams-exist-in-nextjs-layout-files)
1. [How Can I Skip the Root Layout for Some Pages in Next.js?](#6-how-can-i-skip-the-root-layout-for-some-pages-in-nextjs)
1. [How To Set Metadata Tags in App Router?](#7-how-to-set-metadata-tags-in-app-router)
1. [How To Set Metadata Dynamically for an App Router Page?](#8-how-to-set-metadata-dynamically-for-an-app-router-page)
1. [Can I Set Metadata in a Layout File?](#9-can-i-set-metadata-in-a-layout-file)
1. [Can I Use Contexts with App Router?](#10-can-i-use-contexts-with-app-router)
1. [How To Use React Context in Server Components?](#11-how-to-use-react-context-in-server-components)
1. [Is it Possible to Render a Client Component in a Server Component?](#12-is-it-possible-to-render-a-client-component-in-a-server-component)
1. [Can I Pass Props from a Server Component to a Client Component?](#13-can-i-pass-props-from-a-server-component-to-a-client-component)
1. [Can I Use Server Components in Client Components?](#14-can-i-use-server-components-in-client-components)
1. [Can I Pass Props from a Client Component to a Server Component?](#15-can-i-pass-props-from-a-client-component-to-a-server-component)
1. [Can I Use Server Actions in Server Components?](#16-can-i-use-server-actions-in-server-components)
1. [Can I Use Server Actions in Client Components?](#17-can-i-use-server-actions-in-client-components)
1. [Are Server Actions Stable in Next.js?](#18-are-server-actions-stable-in-nextjs)

{% embed https://dev.to/perssondennis/how-to-make-a-hero-image-in-nextjs-13-and-14-399k %}

## 1. How To Read URL Params in a Client Component?
If you need to read the pathname or query params from the URL in a Client Component, you can use the [usePathname](https://nextjs.org/docs/app/api-reference/functions/use-pathname) or [useSearchParams](https://nextjs.org/docs/app/api-reference/functions/use-search-params) to do so.

```react
'use client'

import { usePathname, useSearchParams } from 'next/navigation'

const ClientComponent({ children, href }) {
  const pathname = usePathname()
  const searchParams = useSearchParams()
  const search = searchParams.get('search')

  return (
    <div>Next.js usePathname and useSearchParams example</div>
    <div>{pathname}</div>
    <div>{search}</div>
  )
}

export default ClientComponent
```

If you need to navigate programatically in a Client Component, you can use [useRouter](https://nextjs.org/docs/app/api-reference/functions/use-router). For normal navigation, please use the built in Link component Next.js has.

```react
'use client'

import { useRouter } from 'next/navigation'

const ClientComponent({ children }) {
  const router = useRouter()

  const handleClick = () => {
    router.push('someUrl')
  }

  return (
    <div onClick={handleClick}>Click me</div>
  )
}

export default ClientComponent
```

## 2. How To Read URL Params in a Page File?

Params are automatically passed as props to the page component. You can simply use the provided prop.

```react
const PageComponent({ params, searchParams }) {
  const { slug } = params

  return <div>Next.js URL params in page.tsx file</div>
}

export default PageComponent
```

## 3. How To Read URL Params in a Next.js Layout File?

If you need to read URL params in a layout component file, you have two options. First option is to create a Client Component inside the layout and read the params in the Client Component instead. 

This may sound like a bad option if you use Next.js with the expectations to mostly use Server Components, but it actually is completely natural. The rest of your layout file, can still be rendered as a Server Component, it's just the Client Component itself which will need to be rendered on the client. 

For instance, if you would use URL params to filter articles on a blog with the help of chips components, then only the component containing the chips would need to be a Client Component, the rest of the header could remain a Server Component.

![Client component in server component header](https://www.perssondennis.com/images/articles/answers-to-common-nextjs-questions/client-component-in-server-component-header.webp)
_On the website above, the red rectangle can be a Server Component, while the blue rectangle which depends on URL params can be a Client Component_

The other option, might be the lesser good alternative. This is to add a middleware to your application which reads the URL params and passes it on to your layout component. This will give you flexibility to access the URL params, on the cost of disabling static rendering and client-side caching of your Server Components.

{% embed https://dev.to/perssondennis/the-20-most-common-use-cases-for-javascript-arrays-2j8j %}

## 4. How To Read URL Params in a Server Component?

In a arbitrary Server Component, which is neither a layout component or page component, you can get the URL params by passing them down as props from other components.

Important to remember is that Server Components which aren't page file components aren't normally refreshed during navigation, the components as a whole are serialized and sent to the client and only updates in rare cases. For that reason, you must be careful when using information from the URL in Server Components. The URL params will get outdated upon navigation.

## 5. Why Doesn't searchParams Exist in Next.js Layout Files?

searchParams are only available in page files for a reason. Page files will always re-render when you navigate between two pages. Since the component re-renders when the URL changes, we can for sure know that the URL params passed to the component are up to date.

The purpose of layout components, is to avoid unnecessary renderings and to contain static parts of the UI which aren't updated when the user navigates between pages. A layout component does therefore only re-render when the user navigates to another part of the screen where that component isn't visible. In the code, that means to navigate between two pages which don't share a common layout file as ancestor in the file tree.

Effectively, this means that the URL will change more often than layout files renders, and if we were to read URL params in a layout component, the component [wouldn't get to know when the URL params updated](https://nextjs.org/docs/app/api-reference/file-conventions/layout#layouts-do-not-receive-searchparams). Remember that the layout file is a Server Component, it is rendered to HTML when it is first fetched and then cached on client so it never has to re-render.

To learn more, read the section above about how to read URL params in a Next.js layout file. 

## 6. How Can I Skip the Root Layout for Some Pages in Next.js?

A Next.js project can have many layout files, technically each page can have its own. The top level layout file which wraps your whole application is called Root Layout.

Layout files allow you to re-use a component as a wrapper component for several pages. Most commonly, each Next.js applications has a Root Layout file which includes the applications main menu, such as a header menu or hero image.

While doing that, you will soon run into a question; can you skip the Root Layout for specific pages? And the answer is, yes you can. Next.js has a feature called [Route Groups](https://nextjs.org/docs/app/building-your-application/routing/route-groups#creating-multiple-root-layouts) which allows you to have multiple Root Layouts. This means that different parts of your application can have different layouts, which also makes it possible to skip having any components at all in the layout for certain pages.

One thing to note here is that navigation between route groups will trigger a full page load. That is a natural consequence of replacing the top level Server Component in the application. It basically means, you render a completely new part of your application which is independent from the rest of all pages.

{% embed https://dev.to/perssondennis/why-server-components-a-brief-history-of-web-198h %}

## 7. How To Set Metadata Tags in App Router?

Next.js makes it very easy for you to set metadata for your app and pages. The only thing you have to do is to [export a metadata object](https://nextjs.org/docs/app/building-your-application/optimizing/metadata#static-metadata) from any layout or page file in your application to set the metadata for that layout/page and down.

```react
import type { Metadata } from 'next'

export const metadata: Metadata = {
  title: 'Next.js Metadata Example',
  description: 'Example of metadata in Next.js',
}

export default function MyPage() { ... }
```

If you need dynamic data, see the question below.

## 8. How To Set Metadata Dynamically for an App Router Page?

For most SEO optimized applications, setting static metadata isn't enough, since data will be unique to almost every page and even be loaded at runtime from a server. To set metadata dynamically in a Next.js page, use the [provided generateMetadata function](https://nextjs.org/docs/app/building-your-application/optimizing/metadata#dynamic-metadata).

```react
import type { Metadata } from 'next'

export async function generateMetadata(): Promise<Metadata> {
  const data = await fetch('someUrl').then((res) => res.json())

  return {
    title: 'Next.js Dynamic Metadata Example',
    description: data.pageDescription,
  }
}

export default function MyPage() { ... }
```

Good to know is that the fetch function in Next.js by default caches duplicated requests. This is a general behavior, it isn't specific to metadata. Even requests in Server Components are cached when fetch is used. Although, it can be opted out from and even disabled automatically when certain Next.js features are used.

## 9. Can I Set Metadata in a Layout File?

Yes, you can set metadata in a layout file, both static and dynamically. Please see the questions about setting metadata above.

## 10. Can I Use Contexts with App Router?

It is possible to use React Contexts with the App Router in Next.js. You can preferably place the contexts in the root layout to make it available to all Client Components. The context will not be available to use in Server Components, and it will not mess up the render behavior of Server Components.

You can read about how it works in [this article](https://www.perssondennis.com/articles/do-contexts-in-nextjs-13-make-the-whole-app-render-on-the-client).

{% embed https://dev.to/perssondennis/do-contexts-in-nextjs-13-make-the-whole-app-render-on-the-client-55bb %}

## 11. How To Use React Context in Server Components?

In Next.js, you can only use React contexts in Client Components. Server Components will not be affected by contexts in Client Components and it cannot read the context. See "[Do Contexts in Next.js 13 Make the Whole App Render on the Client?](https://www.perssondennis.com/articles/do-contexts-in-nextjs-13-make-the-whole-app-render-on-the-client)" for more information.

However, dependent on your need, you might be fine with not having a context in your Server Component. By default, Next.js caches data retrieved in requests sent with the fetch function, meaning that you don't have to worry about duplicated request. If multiple Server Components need to fetch the same data, you can rely on the cache as an alternative to a sharing data through a context.

## 12. Is it Possible to Render a Client Component in a Server Component?

It's completely normal to [render Client Components in Server Components](https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#passing-props-from-server-to-client-components-serialization). All props which are passed to the Client Component must be serializable though. Data which isn't serializable must be handled from within the Client Component instead.

## 13. Can I Pass Props from a Server Component to a Client Component?

Yes it is possible to pass props from a Server Component to a Client Component. As mentioned in the previous question, the data must be serializable.

## 14. Can I Use Server Components in Client Components?

It is possible to user Server Components in a Client Component, but with limitations. You [cannot import Server Components in Client Components as usual](https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#unsupported-pattern-importing-server-components-into-client-components), they will not be Server Components when doing so. What you will have to do, is to [pass the Server Component as a prop or child to the Client Component](https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#supported-pattern-passing-server-components-to-client-components-as-props).

Note specifically, because this tricks many developers. Next.js claim that components are Server Components by default, and that you have to use the "use client" directive to make it a Client Component. That is not the whole truth, because when you import any component in a Client Component, the imported component will be a Client Component, not a Server Component. 

As mentioned above, you have to pass the Server Component as a prop if it should be treated as a Server Component. Next.js don't complain if you import what you think are Server Components into Client Components, they are simply just treated as Client Components without any warnings. So if you think you are using a lot of Server Components, you may actually not have that many at all.

{% embed https://dev.to/perssondennis/how-to-use-mvvm-in-react-using-hooks-and-typescript-3o4m %}

## 15. Can I Pass Props from a Client Component to a Server Component?

No, it isn't possible to pass props from a Client Component to a Server Component. Server Components are rendered into HTML on server already before Client Components are rendered.

## 16. Can I Use Server Actions in Server Components?

Yes of course, they are made for that. See [this example in the docs](https://nextjs.org/docs/app/api-reference/functions/server-actions#with-server-components).

## 17. Can I Use Server Actions in Client Components?

Yes, but with a caveat. You cannot use server actions directly in your Client Component. What you can do, is can write the server action in a separate file, and use it in a Client Component, like shown in [the docs](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#client-components).

However, if you try to use server actions directly in a component like it is done in a Server Component, then you would get this error.

> It is not allowed to define inline "use server" annotated Server Actions in Client Components. │ To use Server Actions in a Client Component, you can either export them from a separate file with "use server" at the top, or pass them down through props from a Server Component.

As you can read there, you have one more option. You can define the server action in a Server Component and pass it as a prop to the Client Component if you would want that.

## 18. Are Server Actions Stable in Next.js?

In Next.js 14 server actions are considered stable. One thing to note though, Next.js is built upon React features which are still marked as [experimental](https://react.dev/reference/react/use-server).

Server actions were experimental back in Next.js 13, so if in version 13, you will be asked to add server actions as an experimental feature in next.config.js

> to use server actions, please enable the feature flag in your next.js config.

This is what you would need to add to next.config.js. Although I rather recommend to upgrade to Next.js 14.

    experimental: {
      serverActions: true,
    },

{% embed https://dev.to/perssondennis %}