Getting Started - Server and Client Components - Next - Js
Getting Started - Server and Client Components - Next - Js
js
Menu
Copy page
This page explains how Server and Client Components work in Next.js and when to use
them, with examples of how to compose them together in your application.
For example, the <Page> component is a Server Component that fetches data about a
post, and passes it as props to the <LikeButton> which handles client-side interactivity.
app/[id]/page.tsx TypeScript
return (
<div>
<main>
<h1>{post.title}</h1>
{/* ... */}
<LikeButton likes={post.likes} />
</main>
</div>
)
}
app/ui/like-button.tsx TypeScript
'use client'
https://nextjs.org/docs/app/getting-started/server-and-client-components 2/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
- Server Components are rendered into a special data format called the React Server
Component Payload (RSC Payload).
- Client Components and the RSC Payload are used to prerender HTML.
1. HTML is used to immediately show a fast non-interactive preview of the route to the
user.
2. RSC Payload is used to reconcile the Client and Server Component trees.
3. JavaScript is used to hydrate Client Components and make the application interactive.
What is hydration?
Hydration is React's process for attaching event handlers to the DOM, to make the static HTML
interactive.
Subsequent Navigations
On subsequent navigations:
https://nextjs.org/docs/app/getting-started/server-and-client-components 3/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
Examples
Using Client Components
You can create a Client Component by adding the "use client" directive at the top of
the file, above your imports.
app/ui/counter.tsx TypeScript
'use client'
return (
<div>
<p>{count} likes</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
)
}
"use client" is used to declare a boundary between the Server and Client module
graphs (trees).
Once a file is marked with "use client" , all its imports and child components are
considered part of the client bundle. This means you don't need to add the directive to
every component that is intended for the client.
For example, the <Layout> component contains mostly static elements like a logo and
navigation links, but includes an interactive search bar. <Search /> is interactive and
needs to be a Client Component, however, the rest of the layout can remain a Server
Component.
app/layout.tsx TypeScript
// Client Component
import Search from './search'
// Server Component
import Logo from './logo'
app/ui/search.tsx TypeScript
'use client'
app/[id]/page.tsx TypeScript
https://nextjs.org/docs/app/getting-started/server-and-client-components 5/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
const post = await getPost(id)
app/ui/like-button.tsx TypeScript
'use client'
Alternatively, you can stream data from a Server Component to a Client Component with
the use Hook . See an example.
app/ui/modal.tsx TypeScript
https://nextjs.org/docs/app/getting-started/server-and-client-components 6/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
'use client'
Then, in a parent Server Component (e.g. <Page> ), you can pass a <Cart> as the child of
the <Modal> :
app/page.tsx TypeScript
In this pattern, all Server Components will be rendered on the server ahead of time,
including those as props. The resulting RSC payload will contain references of where
Client Components should be rendered within the component tree.
Context providers
React context is commonly used to share global state like the current theme. However,
React context is not supported in Server Components.
app/theme-provider.tsx TypeScript
'use client'
https://nextjs.org/docs/app/getting-started/server-and-client-components 7/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
children,
}: {
children: React.ReactNode
}) {
return <ThemeContext.Provider value="dark">{children}</ThemeContext.Provider>
}
app/layout.tsx TypeScript
Your Server Component will now be able to directly render your provider, and all other
Client Components throughout your app will be able to consume this context.
Good to know: You should render providers as deep as possible in the tree – notice how
ThemeProvider only wraps {children} instead of the entire <html> document. This makes it
easier for Next.js to optimize the static parts of your Server Components.
Third-party components
When using a third-party component that relies on client-only features, you can wrap it in
a Client Component to ensure it works as expected.
For example, the <Carousel /> can be imported from the acme-carousel package. This
component uses useState , but it doesn't yet have the "use client" directive.
https://nextjs.org/docs/app/getting-started/server-and-client-components 8/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
If you use <Carousel /> within a Client Component, it will work as expected:
app/gallery.tsx TypeScript
'use client'
return (
<div>
<button onClick={() => setIsOpen(true)}>View pictures</button>
{/* Works, since Carousel is used within a Client Component */}
{isOpen && <Carousel />}
</div>
)
}
However, if you try to use it directly within a Server Component, you'll see an error. This is
because Next.js doesn't know <Carousel /> is using client-only features.
To fix this, you can wrap third-party components that rely on client-only features in your
own Client Components:
app/carousel.tsx TypeScript
'use client'
Now, you can use <Carousel /> directly within a Server Component:
app/page.tsx TypeScript
https://nextjs.org/docs/app/getting-started/server-and-client-components 9/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
<div>
<p>View pictures</p>
{/* Works, since Carousel is a Client Component */}
<Carousel />
</div>
)
}
lib/data.ts TypeScript
return res.json()
}
This function contains an API_KEY that should never be exposed to the client.
In Next.js, only environment variables prefixed with NEXT_PUBLIC_ are included in the
client bundle. If variables are not prefixed, Next.js replaces them with an empty string.
https://nextjs.org/docs/app/getting-started/server-and-client-components 10/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
As a result, even though getData() can be imported and executed on the client, it won't
work as expected.
To prevent accidental usage in Client Components, you can use the server-only package
.
Then, import the package into a file that contains server-only code:
lib/data.js
import 'server-only'
return res.json()
}
Now, if you try to import the module into a Client Component, there will be a build-time
error.
The corresponding client-only package can be used to mark modules that contain
client-only logic like code that accesses the window object.
Terminal
Next.js handles server-only and client-only imports internally to provide clearer error
messages when a module is used in the wrong environment. The contents of these
packages from NPM are not used by Next.js.
https://nextjs.org/docs/app/getting-started/server-and-client-components 11/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
Next.js also provides its own type declarations for server-only and client-only , for
TypeScript configurations where noUncheckedSideEffectImports is active.
Next Steps
Learn more about the APIs mentioned in this page.
use client
Learn how to use the use client directive
to render a component on the client.
Previous Next
Linking and Navigating Partial Prerendering
https://nextjs.org/docs/app/getting-started/server-and-client-components 12/13
2025/9/5 14:43 Getting Started: Server and Client Components | Next.js
[email protected] Subscribe
https://nextjs.org/docs/app/getting-started/server-and-client-components 13/13