0
0
NextJSframework~10 mins

Server-side state passing in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Server-side state passing
User requests page
Server runs server component
Fetch or compute data on server
Pass data as props to client component
Client component renders with data
User sees page with server data
The server receives a request, runs server code to get data, then passes that data to client components for rendering.
Execution Sample
NextJS
export default async function Page() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return <ClientComponent data={data} />;
}

'use client';

function ClientComponent({ data }) {
  return <div>{data.message}</div>;
}
This code fetches data on the server, then passes it as a prop to a client component that renders it.
Execution Table
StepActionData StateComponent RenderedOutput
1User requests pageNo data yetNo componentLoading...
2Server fetches datadata = { message: 'Hello from server' }No componentLoading...
3Server renders Page componentdata passed as propPage renders ClientComponent<div>Hello from server</div>
4Client receives rendered HTMLdata present in propsClientComponent hydrated<div>Hello from server</div>
5User sees pagedata displayedClientComponent visibleHello from server
💡 Rendering completes after server data is passed and client component renders with it.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
dataundefined{ message: 'Hello from server' }{ message: 'Hello from server' }{ message: 'Hello from server' }
Key Moments - 3 Insights
Why does the server fetch data before rendering the component?
Because the server component runs first and needs data to pass as props to the client component, as shown in step 2 and 3 of the execution table.
How does the client component get the server data?
The server passes the fetched data as props to the client component during rendering, so the client component receives it ready to display, as seen in step 3.
Why is the user not seeing 'Loading...' after the server fetch?
Because the server waits for data before rendering and sending HTML, so the user only sees the final rendered output, not intermediate loading states.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'data' after step 2?
Anull
B{ message: 'Hello from server' }
Cundefined
D{}
💡 Hint
Check the 'Data State' column in step 2 of the execution table.
At which step does the ClientComponent first render with data?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Component Rendered' column to see when ClientComponent renders.
If the server fetch fails and returns no data, what would change in the execution table?
AUser would see the final output as normal
BClientComponent would render with data anyway
CData State at step 2 would be undefined or error
DStep 3 would be skipped
💡 Hint
Consider what happens if fetch does not return data at step 2.
Concept Snapshot
Server-side state passing in Next.js:
- Server components fetch or compute data first
- Data passed as props to client components
- Client components render with server data
- User sees fully rendered page with data
- No loading state on initial load
Full Transcript
In Next.js, when a user requests a page, the server runs the server component first. This server component fetches or computes the needed data. Once the data is ready, it passes it as props to client components. The client components then render using this data. The user receives a fully rendered page with the server data included, so they do not see loading states. This flow ensures fast, data-ready pages on first load.