08 / 09

How to use context with server components?

Difficulty: 7/10
React Server Components, Context API, Data Fetching

Context cannot be directly used within Server Components in Next.js due to their stateless nature. However, context can be made available to Server Components by creating a client component wrapper.

Create a Context and Provider in a Client Component:
  1. 1

    Create a new file, for example, context/MyContext.tsx.

  2. 2

    Mark the component as a client component using the 'use client' directive at the top of the file.

  3. 3

    Import createContext and useState from React.

  4. 4

    Create the context using createContext().

  5. 5

    Create a provider component that wraps its children with the context provider and manages the context value using useState.

  6. 6

    Export the context and the provider component.

javascript
Wrap Server Components with the Provider:
  1. 1

    In a parent component, such as a layout or page, import the provider component.

  2. 2

    Wrap the desired Server Components with the provider.

javascript
use the context in client components using useContext hook

Scenario Questions

0-2 years experience

  1. 1You need to display a user's name in a server component that receives it via React context. How would you set up the provider and consume the value in the server component?
  2. 2If you forget to wrap a server component tree with the context provider, what will the component render when it tries to read the context?
  3. 3Show me the minimal code to create a context that works in a server component and pass a static value.

2-5 years experience

  1. 1We have a page that fetches product data on the server and wants to share the fetched data across multiple nested server components using context. Walk me through how you'd implement that and what pitfalls to watch for.
  2. 2During a recent deployment, a server component that consumed a context started throwing 'Invalid hook call' errors. What could cause that, and how would you debug it?
  3. 3Explain why you might choose to use context instead of passing props down several layers in a server component hierarchy, and what trade‑offs that introduces.

5-8 years experience

  1. 1Our application renders a large list of items using server components, and we want to provide a theme context that influences rendering of each item. How would you design the context to avoid unnecessary re‑rendering and keep streaming performance optimal?
  2. 2When integrating server components with client components that also consume the same context, what edge cases arise and how would you ensure consistent values across the boundary?
  3. 3If we need to support per‑request scoped data (e.g., auth info) via context in a server component tree that is rendered concurrently for many users, what considerations around memory usage and isolation must you address?

8+ years experience

  1. 1We are planning to migrate a legacy monolith to a React Server Components architecture and want to introduce a global request‑level context for feature flags, locale, and auth. How would you structure that context layer to be maintainable across multiple teams and avoid coupling?
  2. 2Describe how you would implement a context propagation mechanism that works both for server components during streaming and for client components after hydration, ensuring that updates are correctly synchronized without breaking the server‑side rendering pipeline.
  3. 3What are the long‑term performance and observability implications of using deep context trees in server components, and how would you monitor or refactor them as the codebase scales?

Follow-up Questions

  • What alternatives did you consider, and why did you choose this approach?
  • How would you test that the context propagates correctly in both server and client environments?
  • Can you think of any edge cases where this pattern could introduce bugs or performance regressions?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.