Sub topic 9
How hidden trees update at lower priority
This chapter covers what React does when props, context, or state change inside a hidden Activity boundary. It builds on the earlier chapters, so you should already know the following:
Activityand itsmodeprop ('visible'or'hidden')- Hidden state and DOM are preserved
- Hidden Effects are cleaned up
- React 19.2 or later, which includes
Activity
The question this chapter answers
Earlier chapters showed that a hidden Activity keeps its state and DOM nodes, and that React runs Effect cleanups when the boundary becomes hidden. That raises a practical question. If the tree is hidden, does React still render it when something changes?
The answer is yes, but with lower priority. Hidden children still re-render in response to new props, context changes, and state updates. React schedules that work behind everything the user can see. Visible content is never held up by a hidden tree.
Rendering is not the same as running Effects
Rendering and Effects are two separate phases, and Activity treats them differently when it hides a tree.
| Aspect | Hidden `Activity` behavior |
|---|---|
| State | Preserved |
| DOM nodes | Kept in the page, hidden with `display: none` |
| Effects and layout Effects | Cleaned up, not running |
| Re-rendering on new props or context | Still happens, at lower priority |
| Priority relative to visible content | Lower. Visible updates go first. |
Rendering means React calls your component function and works out what the output should be. In a hidden tree this still happens when inputs change. Effects are the code that touches the outside world: timers, subscriptions, network connections. These are stopped.
The result is a tree that keeps its output up to date, so it is ready when the user returns, without doing any outside-world work in the meantime.
Seeing it happen
The following program makes the behavior visible. A parent holds a counter and passes it to a hidden child. The child logs each time it renders.
import { Activity, useState } from 'react';
function Report({ count }) {
console.log('Report rendered with count', count);
return <p>Report sees count: {count}</p>;
}
export default function App() {
const [count, setCount] = useState(0);
const [showReport, setShowReport] = useState(false);
return (
<>
<button onClick={() => setCount(count + 1)}>Increment ({count})</button>
<button onClick={() => setShowReport(!showReport)}>Toggle report</button>
<Activity mode={showReport ? 'visible' : 'hidden'}>
<Report count={count} />
</Activity>
</>
);
}Open the browser console and click Increment several times while the report is hidden. You will see Report rendered with count ... lines appear, even though nothing on screen changes. The <p> is in the DOM with display: none, and its text is being kept current.
Now click Toggle report. The text shows the latest count immediately. The child has no stale output to catch up on, because the work was already done in the background.
If the Activity were replaced with {showReport && <Report count={count} />}, nothing would render while hidden. On reveal, React would mount Report from scratch.
What "lower priority" means
React does not render everything in the order updates arrive. Each update is assigned to a lane, which is a priority class. A click handler's setState gets a high-priority lane because the user expects an immediate response. Updates inside startTransition get lower-priority transition lanes. Work in a hidden Activity tree is placed in a still lower class, behind the lanes for visible content.
In practice, this has three consequences:
- When a visible update and a hidden update are both pending, React renders the visible update first and commits it.
- Hidden rendering is interruptible. If a new visible update arrives while React is rendering the hidden tree, React can pause or discard that hidden render and handle the visible one first.
- Hidden work happens when React has spare time. On a busy page, the hidden tree can lag behind the latest props for a while.
You do not set this priority yourself. Activity has no prop for it. The priority comes from the fact that the tree is hidden, and it changes when you set mode to 'visible'.
State updates inside a hidden tree
The same rule applies to state held inside the hidden component. If a hidden component's state is updated, React processes that update at the lower priority. Because Effects are not running while hidden, most state updates in a hidden tree do not come from inside it. They come from three places:
- New props from a parent that is visible.
- A context value that changed above the boundary.
- A state update triggered by something other than an Effect, such as a handler that a parent passes down and calls.
This is why the earlier example showed the child re-rendering. The child had no Effect doing anything. The new prop was enough.
Here is a version where a visible parent updates a hidden child's own state through a ref-free pattern: the key is passed as a prop, and the child derives its display from it.
import { Activity, useState } from 'react';
function Notes({ draft }) {
const [saved, setSaved] = useState([]);
return (
<section>
<p>Draft preview: {draft}</p>
<button onClick={() => setSaved([...saved, draft])}>Save</button>
<ul>
{saved.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
</section>
);
}
export default function App() {
const [draft, setDraft] = useState('');
const [open, setOpen] = useState(false);
return (
<>
<input value={draft} onChange={(e) => setDraft(e.target.value)} placeholder="Type a draft" />
<button onClick={() => setOpen(!open)}>Toggle notes</button>
<Activity mode={open ? 'visible' : 'hidden'}>
<Notes draft={draft} />
</Activity>
</>
);
}Each keystroke in the visible <input> is high priority. The input stays responsive because the Notes re-render is scheduled behind it. The saved list in Notes persists across hide and show.
Why this design is useful
There are two benefits.
Fresh content on reveal. When the user switches back to a hidden tab, the content is already rendered against current props. Without background rendering, React would have to render the whole tab at the moment of the click, which is the moment the user is waiting.
Protection for the visible UI. If hidden trees updated at the same priority as visible ones, a heavy hidden tab could make typing or scrolling lag. Giving hidden work lower priority keeps the cost off the interactions the user can see.
Gotchas
Render functions must stay pure
Hidden components still render, so any side effect placed in the render body still runs. The common mistakes are mutating a module variable, writing to localStorage, or sending analytics during render.
function Report({ count }) {
// Wrong: this runs on every render, including renders while hidden.
trackPageView('report', count);
return <p>{count}</p>;
}Put such calls in an Effect instead. Effects do not run while the boundary is hidden, so the call only happens when the user can see the report.
import { useEffect } from 'react';
function Report({ count }) {
useEffect(() => {
trackPageView('report', count);
}, [count]);
return <p>{count}</p>;
}
function trackPageView(name, value) {
console.log('view', name, value);
}When the boundary becomes visible, React runs the Effect again, so the view is tracked at the moment of reveal.
Expensive render work is still paid for
Lower priority does not mean free. A hidden tree that does heavy computation on every prop change still uses CPU time, and it can delay other low-priority work. Options for reducing the cost:
- Wrap pure child components in
memoso unchanged props skip the render. - Use
useMemofor expensive derived values. - Pass the hidden tree only the props it needs. A parent that passes a frequently changing value to a hidden subtree causes frequent hidden renders.
- Consider conditional rendering for screens that are costly and rarely revisited. The cost chapter covers this trade-off.
Hidden content can lag
Because hidden rendering is interruptible and low priority, do not assume that hidden output is always up to date at every instant. Do not read from the hidden DOM for decisions that need exact current values, such as measuring layout. A hidden element with display: none has no layout anyway, so measurements return zero.
Do not rely on Effects to keep hidden state fresh
A pattern like "an Effect watches a prop and copies it into state" does not run while hidden. The state is updated only when the Effect runs again after reveal, which can produce a flash of old content. Derive values during render instead.
// Fragile: the copy happens in an Effect, which does not run while hidden.
function Badge({ unread }) {
const [label, setLabel] = useState('');
useEffect(() => setLabel(`${unread} unread`), [unread]);
return <span>{label}</span>;
}
// Better: compute during render, which still happens while hidden.
function BadgeFixed({ unread }) {
return <span>{`${unread} unread`}</span>;
}The second version also avoids an extra render, so it is the better pattern even without Activity.
Connecting to pre-rendering
The previous chapter covered pre-rendering, where an Activity that has never been visible renders its children in the background. That is the same lower-priority mechanism applied to a first render. React builds the tree and its DOM at low priority, skips Effects, and leaves the content hidden until you set mode to 'visible'. Once the boundary has been shown at least once, later prop changes use the update path described in this chapter.
A checklist for hidden-tree updates
Use these questions when you add Activity to a screen:
- Does the component's render function have side effects? Move them into Effects.
- Does the hidden tree receive props that change often? Memoize, or narrow what you pass.
- Does any value depend on an Effect to stay current? Derive it during render.
- Is the hidden screen expensive to render? Measure with the React DevTools Profiler before deciding to keep it alive.
Exercise
A tab panel is wrapped in <Activity mode={tab === 'stats' ? 'visible' : 'hidden'}>. The Stats component receives a data prop that updates once per second from a visible parent, and it contains a large chart that is slow to render. While Stats is hidden, the user reports that typing in a visible search box feels slightly slower than before Activity was added. Explain what is happening and name two changes that would reduce the effect.
Show answer
Hidden children still re-render when their props change, and data changes every second, so Stats re-renders in the background. React gives that work lower priority than the visible search box, but the render still uses CPU time, and a long, slow render can still compete with the main thread. Two ways to reduce it: wrap the chart in memo (and pass stable props) so unchanged inputs skip rendering, or stop passing the fast-changing data to the hidden tree, for example by passing it only when the tab is visible or by using conditional rendering for this rarely visited tab. Memoizing the expensive derived chart data with useMemo also helps.
Exercise
A hidden Activity child calls analytics.send('viewed') directly in its render body. The parent updates its props five times while the child is hidden. How many times is analytics.send called, and what is the fix?
Show answer
It can be called once for every render of the child, including the background renders, so it may be called about five or more times even though the user never saw the screen. The fix is to move the call into a useEffect. Effects are cleaned up and do not run while the boundary is hidden, so the event is sent only when the child is visible.