Sub topic 5
What goes: Effects are cleaned up when hidden
This chapter assumes you know useState, useEffect and cleanup functions, and that you are on React 19.2 or later, where Activity is available. The previous chapter showed what stays when an Activity boundary is hidden: component state and DOM nodes. Here you will see what does not stay: running Effects.
The rule
When mode changes from 'visible' to 'hidden', React keeps the state and the DOM. It also runs the cleanup function of every Effect inside the boundary, as if the component had been unmounted. When the mode changes back to 'visible', React runs those Effects again.
So a hidden tree is not fully "alive". It looks like this:
| Part of the component | While hidden |
|---|---|
| `useState` and `useReducer` values | Kept |
| DOM nodes and their attributes | Kept, but hidden from the screen |
| `useEffect` setup | Cleaned up |
| `useLayoutEffect` setup | Cleaned up |
| Refs | Kept |
This design stops hidden screens from doing work the user cannot see. A hidden screen does not need to poll a server or tick a timer.
A timer that stops while hidden
Here is the Clock component from the topic plan. It starts an interval in an Effect and clears it in the cleanup.
import { useEffect, useState } from 'react';
function Clock() {
const [now, setNow] = useState(() => new Date());
useEffect(() => {
const id = setInterval(() => setNow(new Date()), 1000);
return () => clearInterval(id);
}, []);
return <p>{now.toLocaleTimeString()}</p>;
}Put Clock inside an Activity boundary and the sequence is:
- The boundary is visible. The Effect runs and the interval starts.
- You set
modeto'hidden'. React runs the cleanup, soclearIntervalstops the timer. - While hidden,
nowkeeps its last value. No ticks happen. - You set
modeto'visible'. React runs the Effect again and a new interval starts.
The clock shows the old time for up to one second after it reappears, until the first new tick. Nothing in the component is wrong. It is a consequence of the timer being off while hidden.
Why the cleanup must be complete
React treats hide and show like an unmount and a mount for Effects. That is why the Effect-cleanup habit you already have matters more here. Each setup must have a cleanup that undoes everything it did. If the cleanup is missing or partial, hiding the boundary leaves something running, and showing it again adds a second copy.
A common mistake is a subscription with no cleanup:
import { useEffect, useState } from 'react';
function Status() {
const [online, setOnline] = useState(navigator.onLine);
useEffect(() => {
const update = () => setOnline(navigator.onLine);
window.addEventListener('online', update);
window.addEventListener('offline', update);
return () => {
window.removeEventListener('online', update);
window.removeEventListener('offline', update);
};
}, []);
return <p>{online ? 'Online' : 'Offline'}</p>;
}Without the return function, every hide and show cycle would add another pair of listeners on window. With it, the listeners exist only while the boundary is visible.
What cleanup does not undo
Cleanup only reverses what your Effect did. Anything outside your Effects keeps going. Two examples:
- Work started in an event handler, such as a
fetchcall or asetTimeoutstarted on a click, is not tied to an Effect. Hiding the boundary does not cancel it. - Browser behavior on a kept DOM node, such as a playing
<video>, is not stopped, because the node stays in the page.
The next chapters cover the second case in detail. For now, remember that "Effects are cleaned up" does not mean "everything stops."
Checking your Effects
For each Effect in a component that may sit inside an Activity, ask:
- What does setup start: a timer, a listener, a connection, a subscription?
- Does cleanup stop each of those things?
- Can setup run again safely after cleanup, with the state that was kept?
If all three answers are yes, the component will behave correctly when hidden and shown.
Exercise
A component inside an Activity boundary has useEffect(() => { const ws = new WebSocket(url); ws.onmessage = (e) => setMessages((m) => [...m, e.data]); }, [url]); with no cleanup. The boundary is hidden and then shown again three times. What goes wrong, and how do you fix it?
Show answer
Each time the boundary becomes visible, the Effect runs again and opens a new WebSocket. Nothing closes the earlier ones, because there is no cleanup, so several connections stay open and each one appends messages to the same state. The fix is to return a cleanup function from the Effect: return () => ws.close();. Then hiding the boundary closes the connection, and showing it opens one new connection. The messages state is kept across hiding, so earlier messages remain.