Sub topic 12
Applying payloads on the client and during navigation
This chapter assumes you know React components, props, useState, promises, and the basic shape of an RSC payload (element rows, client references, lazy references). It covers what the browser does after a payload arrives. It also covers why a navigation can replace server-rendered output without resetting client state.
What "applying" a payload means
The payload is not HTML and it is not a finished tree of DOM nodes. It is a serialized description of React elements. The client must turn that description back into React elements, then hand those elements to React to render and reconcile.
Applying a payload takes four steps:
- Read rows from the stream as they arrive.
- Resolve each row into a value. Element rows become React elements. Client reference rows become module loads.
- Build a React element tree, using a promise or lazy placeholder wherever a row has not arrived yet.
- Render that tree. React compares it to what is already on screen and updates only what changed.
The framework, not your application code, normally does this. It calls into the React client runtime (the react-server-dom-* packages, which differ by bundler) to create a tree from a response stream.
Client references become real components
A client reference row says "a component lives in this module, under this export name, in these chunks." In the earlier example, row 1 looks like this:
1:I["app/Counter.js",["chunk-counter.js"],"default"]When the client meets "$L1" in an element's type position, it does the following:
- Asks the bundler runtime to load
chunk-counter.jsif it is not already loaded. - Reads the
defaultexport from the loaded module. - Uses that function as the element type.
Loading is asynchronous, so the element type is wrapped in a lazy value. React treats this like React.lazy. It suspends the nearest Suspense boundary until the module is ready. If a chunk is already in the page, the lookup is immediate and nothing suspends.
This matters for a practical reason. The payload carries only a pointer to client code. The client code itself comes from the bundle, so a component used on ten pages is downloaded once and reused.
Reconciliation is why state survives
The central rule of navigation is this: the client does not throw away the old tree and mount the new one. It gives React a new tree and lets reconciliation compare it with the old one.
Reconciliation matches elements by position, type, and key. When an element in the new tree has the same type at the same position as before, React keeps the existing instance. For a Client Component, that instance holds its useState values and its effects. Receiving new props does not reset them.
Consider a layout that contains a search box and a page body.
// app/layout.js (Server Component)
import SearchBox from './SearchBox';
export default function Layout({ children }) {
return (
<div>
<SearchBox />
{children}
</div>
);
}// app/SearchBox.js
'use client';
import { useState } from 'react';
export default function SearchBox() {
const [query, setQuery] = useState('');
return (
<input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Search"
/>
);
}Suppose the user types "react" and then clicks a link to another page. The new payload describes the same div, the same SearchBox reference, and a different children. After reconciliation, SearchBox is the same instance, so "react" is still in the input. Only the children subtree changes.
If the layout were not part of the new tree, or if a different component type appeared at that position, React would unmount the old one and its state would be lost. The state survives only because the new payload keeps the element type and position the same.
What happens during a navigation
Here is the sequence when the user clicks a link in a framework that uses RSC, such as Next.js with the App Router. The details differ between frameworks.
- The framework's client router intercepts the click, so the browser does not do a full page load.
- The router requests the next route from the server and asks for the RSC payload, not for an HTML page. Frameworks typically do this with a
fetchand a special request header or query parameter. - The server renders the route's Server Components and streams the payload back.
- The router reads the stream, builds an element tree, and renders it inside a React transition.
- When enough of the tree is ready, React commits the update. The URL changes through the History API.
Transitions keep the old UI visible
Routers usually wrap the update in startTransition. A transition tells React that the update is not urgent. If the new tree suspends, because a row is missing or a chunk is loading, React keeps showing the current UI instead of replacing it with a Suspense fallback. The user sees the old page until the new one is ready enough to show.
You can observe this with useTransition. The sketch below shows the principle. Real routers expose this through their own hooks.
'use client';
import { useTransition } from 'react';
export default function NavButton({ go }) {
const [isPending, startTransition] = useTransition();
return (
<button
disabled={isPending}
onClick={() => startTransition(() => go())}
>
{isPending ? 'Loading...' : 'Next page'}
</button>
);
}Here go is a function the app supplies. It must start the navigation and be a Client Component prop passed from another Client Component, because functions cannot cross from server to client.
Partial updates and shared layouts
A well-designed router does not refetch what it already has. Two behaviors are common:
- Shared layouts are reused. When you move between two pages under the same layout, the client can keep the layout's rendered output and request only the segments that differ. The layout's Client Components keep their state.
- Prefetching. When a link scrolls into view or the user hovers over it, the router can fetch the payload ahead of time and keep it in a cache. The later click applies the cached payload with no network wait.
Both behaviors are decisions made by the framework. Plain React does not define a router cache. Check your framework's documentation for what it caches and for how long.
Refreshing server data without losing state
A related operation re-requests the current route's payload and applies it over the current tree. Frameworks expose this as a refresh (in Next.js, router.refresh()). The server re-runs the Server Components with fresh data, and the client reconciles the result.
Because reconciliation preserves matching Client Component instances, a refresh can update a server-rendered list while a client-side form keeps its half-typed input. Server Functions use the same mechanism. After a function such as addPost finishes, the framework can return a fresh payload in the same response and apply it, so the page shows the new data without a manual refetch.
Common errors and gotchas
| Symptom | Likely cause | Fix |
|---|---|---|
| Client state resets on every navigation | The component's type or position changes between payloads, or a different `key` is used | Keep the component at the same position in a stable layout, and avoid changing keys |
| Old page flashes to a fallback during navigation | The update was not wrapped in a transition | Use the framework's router link or navigation API, which uses transitions |
| Page shows stale server data after a mutation | The client cache still holds the old payload | Refresh the route or use the framework's revalidation API |
| Error about a missing module or chunk after a deploy | The client holds a payload that points to chunk names from an older build | Reload the page, and configure deployment so old assets stay available for a while |
| Hydration or render mismatch in a Client Component | Client output differs from the initial server HTML | Move browser-only values into effects |
Version skew
Version skew deserves attention. A payload row names chunk files such as chunk-counter.js. If the server is redeployed while a user still has the old page open, a later navigation may receive payload rows that refer to chunks the client does not know, or the reverse. Frameworks reduce this problem with build identifiers and by falling back to a full page load when the build changes. If you manage your own hosting, keep previous assets available for a short time after each deploy.
Errors inside a payload
A Server Component can throw during rendering. If this happens after the stream has started, the server cannot change the response status. Instead, it encodes the error in a row, and the client rethrows it at the position where the failing element would have appeared. An error boundary above that position catches it. Place error boundaries around parts of the page that can fail independently, so one failure does not blank the whole route.
Putting it together
A navigation fetches a new payload and builds a tree of React elements. Client references in it load from the bundle. React reconciles the new tree against the old one inside a transition. Elements that keep their type and position keep their state, and the rest are replaced. The server decides what to send, and the client keeps what it can.
Exercise
A layout renders <SearchBox /> (a Client Component with useState) above {children}. The user types text into the search box, then clicks a link to a sibling page under the same layout. The text disappears. Give two possible causes related to how payloads are applied.
Show answer
The new payload may place a different element type, or the same type at a different position, where SearchBox was, so React unmounts and remounts it. Alternatively, a key on SearchBox or an ancestor changed between payloads, or the navigation caused a full page load (for example, a plain <a> tag or a build change) instead of a client-side payload fetch, which discards all client state.
Exercise
Why do routers usually apply a fetched payload inside startTransition, and what does the user see while a client chunk for the new page is still loading?
Show answer
A transition marks the update as non-urgent. When the new tree suspends because a row or chunk is not ready, React keeps showing the current UI instead of switching to a Suspense fallback. The user sees the old page until the new one can be shown.