Third-party components and context providers

Intermediate · 5 min

Why context and some libraries break in Server Components, and how to wrap them in a small Client Component, place providers deep, and author libraries.

Before thisThe use client boundaryComposing Server and Client Components

Codevolution · 5 min

Third-party components and context providers

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • React context is not supported in Server Components, so a provider has to live in a Client Component that accepts `children` and is imported into a Server Component layout.
  • Render providers as deep in the tree as you can, wrapping `{children}` rather than the whole `<html>` document, so more of the page can stay static.
  • A third-party component that uses state or effects but lacks a `"use client"` directive fails in a Server Component; wrap it once in your own Client Component and use the wrapper.
  • Library authors should put `"use client"` on entry points that need the browser, so consumers can import them directly into Server Components.

Two problems, one fix

You have a theme that many components read through React context. You install a carousel library from npm. In a plain React app both just work. In the App Router both fail in confusing ways, and for the same reason: the code uses a client-only feature, and the file it sits in has not told Next.js that it is client code.

In both cases the fix is the same small move: put the offending code behind a "use client" boundary that you control.

Context providers

React context is built on client-only machinery, so a provider cannot live in a Server Component. Create the provider in its own Client Component that accepts children:

// components/theme-provider.tsx
"use client";
import { createContext, useState } from "react";

export const ThemeContext = createContext<"light" | "dark">("light");

export function ThemeProvider({ children }: { children: React.ReactNode }) {
  const [theme] = useState<"light" | "dark">("light");
  return <ThemeContext value={theme}>{children}</ThemeContext>;
}

In React 19 you render the context itself as the provider, <ThemeContext value={...}>, with no .Provider. Then import it into your Server Component layout:

// app/layout.tsx
import { ThemeProvider } from "@/components/theme-provider";

export default function RootLayout({ children }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>
        <ThemeProvider>{children}</ThemeProvider>
      </body>
    </html>
  );
}

This works because of the composition rule from the previous lesson: children passes through the provider as a slot, so the pages inside it stay Server Components. The provider is a client island wrapped around server-rendered content. Any Client Component below can now read the context; Server Components cannot, and read their data through props or by fetching. The mechanics of context itself are in the React roadmap’s context and avoiding prop drilling.

Notice the provider wraps {children}, not <html>. The docs advise rendering providers as deep as possible, so Next.js can optimize the static parts above it. I built this layout on Next.js 16 and it compiled and prerendered.

Third-party components

Libraries written before Server Components often use useState or useEffect without a "use client" directive in their own source, since the directive did not exist. Use one directly in a Server Component and Next.js has no way to know it needs the browser, so you get an error.

If you import the library inside a Client Component, it works, because that file already declared the boundary. So wrap it once:

// components/carousel.tsx
"use client";

import { Carousel } from "acme-carousel";

export default Carousel;

Now any Server Component can import @/components/carousel and render it. One wrapper file per library component is the usual pattern, and it keeps the boundary visible in your codebase.

You can pass server-rendered output into a wrapped component as children or as a prop, using the composition pattern from before.

If you write the library

For authors of component libraries, the advice is to put "use client" at the top of entry files that rely on browser features. Then consumers can import directly into Server Components with no wrapper. Some bundlers strip such directives from build output, so check your tool’s configuration to keep them.

What the video covers

The pick is Codevolution’s context providers video, with chapters for context in React, setting up a theme context, creating a separate Client Component for it, simplifying the layout with a ThemeProvider, testing it in a client route page, and key takeaways. It is the context half of this lesson. The third-party half is in the video linked under deeper, which installs react-slick, tests the carousel in a Server Component, and encapsulates it in a client wrapper. Both are Next.js 15.

Where this fits

This closes the server and client components group. The next group, data fetching and caching, starts with fetching data in Server Components.

Check yourself

  1. 01Why can't you create a context provider directly in `app/layout.tsx`?

    Layouts are Server Components by default, and context uses client-only features. The provider must be defined in a Client Component, then imported into the layout and given `children`.

  2. 02How do you use a carousel library that uses `useState` inside a Server Component page?

    Create a small Client Component file with `"use client"` that re-exports or renders the library component, and import that wrapper into the page. The wrapper marks the boundary the library did not.

  3. 03Why place a `ThemeProvider` around `{children}` instead of around `<html>`?

    The deeper the provider sits, the more of the tree above it stays a plain Server Component that Next.js can optimize and prerender. Wrapping the whole document makes less of it static for no gain.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark