Course contentsShow
← React
Lesson 1543 of 2,39638. Server Components FundamentalsFree lesson

Context Providers Must Be Client Components

Why Context and hooks require 'use client', but consumers can receive context through the tree.

Context Providers Must Be Client Components

What you'll learn: Why Context providers require 'use client', even though the components that consume that context can remain Server Components.

The Core Problem

Context in React is built on hooks like createContext, useContext, and relies on runtime state management. Since Server Components run once on the server and don't maintain interactive state, anything that creates or provides Context must be a Client Component.

Here's the key insight: the provider needs 'use client', but the children receiving props through the tree don't.

Why Providers Need Client Boundaries

When you write:

'use client';
import { createContext, useState } from 'react';

export const ThemeContext = createContext();

export function ThemeProvider({ children }) {
  const [theme, setTheme] = useState('light');
  
  return (
    <ThemeContext.Provider value={{ theme, setTheme }}>
      {children}
    </ThemeContext.Provider>
  );
}

This must be a Client Component because:

  • useState manages interactive state
  • The Provider needs to re-render when theme changes
  • Context requires the React runtime on the client

The Children Can Still Be Server Components

Here's the beautiful part: the children prop can contain Server Components. The provider wraps them, but doesn't force them to become client components. Only components that consume the context with useContext need 'use client'.

Key Takeaway: Context providers create client boundaries because they use hooks and state, but they act as "wrapper" Client Components that can have Server Component children—only the actual consumers need to be client components too.