Do Contexts in Next.js 13 Make the Whole App Render on the Client?
Tags: nextjs, react, webdev, javascript
Published: 2023-07-30 17:23:26

React contexts are quite crucial for React development. Most projects depends on it, either directly or through libraries. The question is, can they be used with Next.js 13 App Router? And can the application still benefit from the power of Server Components when contexts are used? Let's answer that!

## In This Article

- [Difference Between Client and Server Components](#difference-between-client-and-server-components)
- [Can I Use React Context in Server Components?](#can-i-use-react-context-in-server-components)
- [Will My Complete Next.js Application Be Rendered on Client if I Use React Contexts?](#will-my-complete-nextjs-application-be-rendered-on-client-if-i-use-react-contexts)

{% embed https://dev.to/perssondennis/errors-received-when-migrating-nextjs-13-to-new-app-folder-1of5 %}

## Difference Between Client and Server Components

For this article to make sense, you should have grasp about what the difference between Client Components and Server Components are. In Short, Client Components are regular old React components rendered with JavaScript on the client, while Server Components are React components which are rendered into HTML on the server. The rendered Server Components can then be used as none-interactive components anywhere in the applications React tree.

Does it make sense? If not, you can learn more about Server Components, read [Next.js docs about it](https://nextjs.org/docs/getting-started/react-essentials) or my article about, [why Server Components matter](https://www.perssondennis.com/articles/why-server-components-a-brief-history-of-web).

## Can I Use React Context in Server Components?

React contexts are well used in React applications to share states between components. Since Client Components and Server Components are rendered on different machines, the client and the server, you might think that React contexts cannot be used.

Server Components are simply HTML markup which certainly don't have a React state. If you try to use a context in a Server Component it will throw an error.

    ReactServerComponentsError:

    You're importing a component that needs useContext. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.

    -[/path/to/file/MyComponent.ts:1:1]
    1 | import { useContext } from 'react'

While Server Components are incompatible with React contexts, Client Components can handle them perfectly fine. As long as you provide the context provider in a Client Component, all Client Components in that component's child-tree will be able to read and use the context. It doesn't matter whether you have any Server Components in the sub-tree or not, Server Components are HTML markup, which don't care!

![Server components don't care meme](https://www.perssondennis.com/images/articles/do-contexts-in-nextjs-13-make-the-whole-app-render-on-the-client/server-components-dont-care.webp)
_It's not you, he's always like that..._

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

## Will My Complete Next.js Application Be Rendered on Client if I Use React Contexts?

The answer is no. Your whole site will not be forced to render on the client if you use React contexts. As mentioned, Server Components do not care about contexts, which also means it doesn't interfere with them.

Let's list a few statements about Next.js App Router.

1. The [RootLayout in Next.js is always a Server Component](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required)
1. The children prop passed to RootLayout is also a Server Component
1. Server Components can include Client Components (the Client Components [will be ignored](https://nextjs.org/docs/getting-started/react-essentials#composing-client-and-server-components) when the server renders the Server Component)
1. Server Components can be included in Client Components if they are [passed as children to the Client Component](https://nextjs.org/docs/getting-started/react-essentials#recommended-pattern-passing-server-components-to-client-components-as-props)

Now look at this code example, it's taken from [Next.js guide for how to set up contexts](https://vercel.com/guides/react-context-state-management-nextjs).

```react
import { ContextProviders } from './providers';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <ContextProviders>{children}</ContextProviders>
      </body>
    </html>
  );
}
```

What we can see in that code is:

- The Root Layout, which is a Server Component by statement #1
- The ContextProviders component which includes the contexts is a Client Component (statement #3 allows that)
- The children prop for the RootLayout is a Server Component (statement #2)
- The children is passed to the ContextProviders Client Component (accepted by statement #4)

By default, Next.js can handle intervened Client and Server Components, and with this setup we are following all the rules of how to use the components. The above code will result in a React tree looking as below.

```xml
<RootLayout (Server Component)>
  <ContextProviders (Client Component)>
    <Page (Server Component)>
      <Potential other Client Components using the context />
      <Potential other Server Components ignoring the context />
    </Page>
  </ContextProviders>
</RootLayout>
```

Although using contexts work fine, it does create a bit of confusion. You must remember that you will only be able to use the context within Client Components, the context is not available on the server.

{% embed https://dev.to/perssondennis/react-anti-patterns-and-best-practices-dos-and-donts-3c2g %}