Sub topic 8
Pre-rendering content the user may open next
This chapter assumes you know the Activity component's mode prop and how Effects are cleaned up when a boundary is hidden. You should also be comfortable with useEffect, lazy, and Suspense. It needs React 19.2 or later.
Starting hidden
So far you have switched an Activity between visible and hidden after the user did something. You can also render an Activity as hidden on its first render. React then pre-renders the children. It builds the component tree and the DOM in the background, before the user has asked to see it.
This lets you prepare the screen the user is most likely to open next. When they open it, the work is already done.
What pre-rendering does
A hidden Activity that has never been visible behaves like this:
- React renders the children and creates their state. The DOM nodes exist but are hidden.
- React does not run the children's Effects. They run for the first time when the boundary becomes visible.
- React renders the hidden tree at a lower priority than the visible UI, so it should not block what the user is doing.
The next example has two tabs. The Reports tab is hidden on first load, so it is pre-rendered.
import { Activity, useEffect, useState } from 'react';
function Reports() {
console.log('Reports rendered');
useEffect(() => {
console.log('Reports Effect ran');
return () => console.log('Reports Effect cleaned up');
}, []);
return <p>Quarterly numbers</p>;
}
export default function App() {
const [tab, setTab] = useState('home');
return (
<>
<button onClick={() => setTab('home')}>Home</button>
<button onClick={() => setTab('reports')}>Reports</button>
<Activity mode={tab === 'home' ? 'visible' : 'hidden'}>
<p>Welcome</p>
</Activity>
<Activity mode={tab === 'reports' ? 'visible' : 'hidden'}>
<Reports />
</Activity>
</>
);
}Open the console and follow the logs:
- On page load you see
Reports rendered. In development with Strict Mode you may see it twice. You do not seeReports Effect ran. - Click Reports. The Effect runs now, and the tab appears.
- Click Home. React runs the cleanup, but the component keeps its state.
The render happened early and the Effect did not.
Why Effects wait
Pre-rendering is meant to be cheap and free of side effects. Effects usually open connections, start timers, or log analytics events. If they ran for a screen nobody can see, you would pay for work that may never matter. Waiting until the boundary is visible keeps pre-rendering safe.
This also means you should not fetch data inside an Effect and expect pre-rendering to do it for you. The Effect does not run while the boundary is hidden.
Loading code and data ahead of time
Pre-rendering helps with data and code that React can read during render. Two common cases work:
- Lazy components. A component created with
lazystarts loading its code when React renders it, even inside a hidden Activity. - Suspense-enabled data. If a library or framework reads data during render, for example with
use, the hidden render can start that request.
import { Activity, lazy, Suspense, useState } from 'react';
const Charts = lazy(() => import('./Charts.jsx'));
export default function Dashboard() {
const [showCharts, setShowCharts] = useState(false);
return (
<>
<button onClick={() => setShowCharts(true)}>Show charts</button>
<Activity mode={showCharts ? 'visible' : 'hidden'}>
<Suspense fallback={<p>Loading charts...</p>}>
<Charts />
</Suspense>
</Activity>
</>
);
}On first render the Activity is hidden, but React still renders Charts. The code for ./Charts.jsx starts downloading in the background. By the time the user clicks, the component may already be loaded and the fallback may never appear.
Choosing what to pre-render
Pre-rendering costs CPU and memory, even at low priority. Apply it where you can predict the user's next move:
- The next tab in a tab bar.
- A details panel for the item the user is hovering over.
- The second step of a short wizard.
Avoid it for large lists of screens the user is unlikely to open. Chapter 10 covers this cost in more detail.
Common errors
- Expecting an Effect to run on pre-render. Effects start only when the boundary is visible.
- Starting a fetch in an Effect and expecting it to be ready. Use a render-time data source, such as
lazyoruse, if you want the work started early. - Pre-rendering too much. Every hidden tree uses memory and render time.
Exercise
A hidden-on-first-render Activity contains a component with a useEffect that calls fetch('/api/report'). The user has not opened it yet. Has the request been sent? What can you change so the data starts loading early?
Show answer
No. Effects do not run in a pre-rendered hidden Activity, so the request is sent only when the boundary becomes visible. To start early, read the data during render with a Suspense-enabled approach, such as use with a cached promise or a data library that supports Suspense. Then the hidden render can begin the request.