Sub topic 12
Applying payloads on the client and during navigation
This chapter assumes you know what the RSC payload is, how client references appear in it, and what a Suspense boundary does. Here you will see what the browser does with a payload once it arrives, and why a navigation can update server-rendered content without losing client state.
From payload to tree
The browser does not receive HTML for Server Components. It receives a stream of rows. React's client runtime reads the stream and rebuilds a React element tree from it.
- Element rows become ordinary React elements, such as
main,h1andul. - A client reference such as
$L1is resolved by loading the chunk that the module row names. The runtime then puts the realCountercomponent into that position. - Props in the payload, such as
initial, become props on the element.
After this step, the result is the same kind of tree that client-only React would build. React then reconciles it into the DOM like any other render.
Navigation in a framework router
In an RSC framework, the router intercepts a link click. It does not load a full page. It asks the server for the payload of the target route and applies it to the tree that is already on screen.
The exact request format and caching rules differ between frameworks. The idea is the same: the unit sent over the network is a payload, not a page.
Why client state survives
The new payload is turned into elements and handed to React as a new render. React compares the new tree with the old one by position and type, as in any update.
- A client component that appears at the same position with the same type stays mounted. Its
useStatevalues and DOM state are kept. - Only the props that came from the server change. If the server sends a new
initialvalue,Counterreceives it, butcountkeeps its current value becauseuseStateignores a changed initial argument. - A component whose type or position changes is unmounted, and its state is lost.
This is why layouts that contain client components, such as a sidebar with an open menu, can stay as they are while the page content changes.
'use client';
import { useState } from 'react';
export default function Sidebar({ links }) {
const [open, setOpen] = useState(false);
return (
<nav>
<button onClick={() => setOpen(!open)}>Menu</button>
{open && (
<ul>
{links.map((l) => (
<li key={l.href}>{l.label}</li>
))}
</ul>
)}
</nav>
);
}If a server layout renders Sidebar and you navigate between pages that share that layout, open stays as it was. The server may send fresh links, and the sidebar will show them.
Streaming and Suspense on navigation
A navigation payload can stream as well. Rows for the parts that are ready arrive first, and rows for parts behind a Suspense boundary arrive later. The router can show the fallback in the meantime. Because the old page is still on screen until the new tree is ready to commit, frameworks often wrap this in a transition so the old UI stays visible instead of flashing a loading state.
Common errors
- Expecting state to reset on navigation. Same type at the same position means the state stays. Add a
keythat changes with the route if you want a reset. - Storing server data in client state through
useState(initial)and expecting it to follow new props. Read the prop directly, or derive the value during render. - Assuming a full reload happened. Module-level variables in client code persist across these navigations.
Exercise
A page's server layout renders a client component Counter with the prop initial. You navigate to another route that uses the same layout, and the server now sends a different initial. What does Counter show, and why?
Show answer
It keeps its current count. The new payload produces an element of the same type at the same position, so React keeps the component mounted and preserves its state. useState uses its initial argument only on the first render, so the new initial prop does not change count.