Sub topic 9
How hidden trees update at lower priority
This chapter assumes you know useState, useEffect and Effect cleanup. You should also know how Activity hides content with its mode prop. Here you will see what React does when state or props change inside a hidden Activity boundary.
Hidden does not mean frozen
A hidden Activity keeps its state and DOM. It also keeps receiving updates. If a parent passes new props to a hidden child, or the child's own state changes, React re-renders that child so it is current when the user opens it.
That raises a question. Should a screen the user can't see slow down the screen they are using? React says no. It renders hidden trees at a lower priority than visible content. Visible updates, such as typing, clicking and animating, are handled first. Hidden work runs afterward, when React has nothing more urgent to do.
What lower priority means
React schedules work by urgency. An update caused by a keystroke is urgent. A re-render of a hidden tree is not, because no one is looking at it. React can interrupt the hidden render and resume it later. Hidden work also never blocks a visible update from reaching the screen.
An example
In the example below, the visible input updates query. A hidden Report panel also reads query and renders a long list.
import { Activity, useState } from 'react';
const ROWS = Array.from({ length: 2000 }, (_, i) => `Row ${i}`);
function Report({ query }) {
const matches = ROWS.filter((row) => row.includes(query));
return (
<ul>
{matches.map((row) => (
<li key={row}>{row}</li>
))}
</ul>
);
}
export default function App() {
const [query, setQuery] = useState('');
const [showReport, setShowReport] = useState(false);
return (
<>
<input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Search"
/>
<button onClick={() => setShowReport(!showReport)}>Toggle report</button>
<Activity mode={showReport ? 'visible' : 'hidden'}>
<Report query={query} />
</Activity>
</>
);
}Type in the input while the report is hidden. The input stays responsive because React finishes the urgent update to value first. The hidden Report catches up afterward. When you press the button, the list already matches your query, so you do not wait for a full render.
Now compare this with conditional rendering. With {showReport && <Report query={query} />}, nothing renders while the report is hidden. Opening it then means mounting the whole list from scratch, which can make the click feel slow.
What still does not happen
Lower priority affects when rendering runs. It does not change which Effects run. While a boundary is hidden:
- Effects are unmounted. Their cleanup functions have already run.
- Effects do not re-run when props change, because they are not mounted.
- Render functions do run, so any expensive calculation in the component body still costs CPU time.
The last point is the most common surprise. Report above filters 2000 rows on every keystroke, even though no one sees the result. Low priority makes that work polite. It does not make it free.
Practical consequences
- Effects are the wrong place for "keep this current" logic in hidden trees. An Effect that syncs data does not run while hidden, so the screen may show old data until it becomes visible again. Derive values during render instead, or refresh in an Effect that re-runs when
modebecomes visible. - Rapid updates add up. A hidden tree that receives a new prop on every keystroke will re-render repeatedly in the background. React may batch or restart these renders, but the total work can still be large.
- Hidden renders can be interrupted. Keep render functions pure, as you already should. A render that React starts and abandons must not leave side effects behind.
Summary of the model
| Situation | Visible tree | Hidden tree |
|---|---|---|
| Props or state change | Re-renders at normal priority | Re-renders at lower priority, after urgent work |
| Effects | Mounted and running | Unmounted, not re-run on change |
| DOM | Shown | Kept in the page, not displayed |
| Render function cost | Paid immediately | Paid later, still paid |
Exercise
A hidden Activity contains a component with useEffect(() => { fetchData(id); }, [id]);. The parent changes id while the component is hidden. Does fetchData run at that moment? What would you check or change if you want the data to be fresh when the user opens the panel?
Show answer
No. React re-renders the hidden component at low priority with the new id, but its Effects are unmounted while hidden, so fetchData does not run. The Effect runs when the boundary becomes visible, because the Effect mounts again with the current id. If the user must not see stale data in that moment, show a loading state while the Effect runs. You could also fetch the data outside the hidden tree, for example in the parent or with a Suspense-enabled data source.