0
0
NextJSframework~10 mins

Server and client component composition in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Server and client component composition
Start: Server Component
Render Server Component
Include Client Component
Send HTML to Browser
Browser Loads Client Component JS
Client Component Hydrates and Runs
User Interacts with Client Component
Client Component Updates UI
End
The server component renders first and includes the client component. The browser receives HTML, then loads and runs the client component for interactivity.
Execution Sample
NextJS
'use client'
import { useState } from 'react'

function ClientComp() {
  const [count, setCount] = useState(0)
  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
}

export default function ServerComp() {
  return (
    <div>
      <h1>Server Side</h1>
      <ClientComp />
    </div>
  )
}
A server component renders a heading and a client component button that counts clicks.
Execution Table
StepActionComponentState BeforeState AfterOutput/Effect
1Start renderingServerCompN/AN/ABegin server render
2Render <h1>ServerCompN/AN/A<h1>Server Side</h1> created
3Render <ClientComp /> placeholderServerCompN/AN/AClient component placeholder inserted
4Send HTML to browserServerCompN/AN/AHTML with <h1> and client placeholder sent
5Browser loads ClientComp JSClientCompcount=undefinedcount=0Client component JS loaded, state initialized
6Render ClientComp buttonClientCompcount=0count=0Button shows 'Clicked 0 times'
7User clicks buttonClientCompcount=0count=1Button updates to 'Clicked 1 times'
8User clicks button againClientCompcount=1count=2Button updates to 'Clicked 2 times'
9No more actionsN/AN/AN/AExecution ends, UI interactive
💡 User stops interacting; client component maintains state on client side.
Variable Tracker
VariableStartAfter Step 5After Step 7After Step 8Final
countundefined0122
Key Moments - 3 Insights
Why does the server component render first before the client component?
Because the server component runs on the server to generate HTML, and the client component is included as a placeholder to hydrate later in the browser (see execution_table steps 1-4).
How does the client component keep track of clicks after the page loads?
The client component uses React state initialized in the browser (count=0 at step 5) and updates it on user clicks (steps 7 and 8), which only happens on the client side.
Why can't the server component handle the button clicks?
Server components do not run in the browser and cannot handle user events; only client components can run JavaScript and respond to interactions (see execution_table steps 7 and 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after the first user click?
A2
B1
C0
Dundefined
💡 Hint
Check the 'State After' column for ClientComp at step 7.
At which step does the browser load the client component JavaScript?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look for the action describing loading ClientComp JS in the execution_table.
If the client component was removed from the server component, what would happen?
ANo button would appear or be interactive.
BThe server component would handle clicks instead.
CThe button would still appear and work.
DThe page would crash.
💡 Hint
Refer to the execution_table steps 3 and 7 where ClientComp is rendered and interacts.
Concept Snapshot
Server components render on the server and send HTML to the browser.
Client components run in the browser for interactivity.
Server components can include client components.
Client components manage state and user events.
This composition allows fast server rendering plus dynamic UI.
Full Transcript
In Next.js, server components run on the server to generate HTML. They can include client components as placeholders. The browser receives the HTML and then loads the client component JavaScript. The client component initializes its state and renders interactive UI elements like buttons. When the user interacts, the client component updates its state and UI without needing to go back to the server. This separation lets the app render fast on the server and stay interactive on the client.