April 23, 2025

NextJS context

when to wrap the children props in the components and to use 'use client' directive

NextJS context

Pitch Details

Comparing All Three Cases: Case 1: AuthContext Provider

Creates a React Context Children need to consume the context with useAuth() RootLayout must be a client component

Case 2: SessionRefresher

Standalone component that doesn't wrap children Just performs side effects RootLayout can remain a server component

Case 3: AuthProvider Wrapper (Current Case)

Wraps children but doesn't create a context Just performs side effects RootLayout can remain a server component

The fundamental difference between Case 1 and Cases 2-3 is whether you're creating a React Context that children will consume. If you're using Context to share state, the entire tree involved needs to be client components. If you're just performing side effects without context, server components can render client components without themselves becoming client components. It's a subtle but important distinction in Next.js's hybrid rendering model.