Activity and useEffectEvent in React 19.2
React 19. 2 came out on October 1.
React 19.2 came out on October 1. It’s a minor release, but two of its additions fix things that have annoyed React developers for years: losing state when you hide part of the UI, and effects that re-run because of a value they only read.
Activity: hide without losing state
The usual way to switch tabs is conditional rendering:
{tab === 'inbox' && <Inbox />}
{tab === 'drafts' && <Drafts />}
Switch away from Drafts and it unmounts. Its state, scroll position and half-typed text are gone. The workarounds were lifting state up, or keeping everything mounted and hiding it with CSS, which leaves effects and subscriptions running in the background.
import { Activity, useState } from 'react';
function Mailbox() {
const [tab, setTab] = useState('inbox');
return (
<>
<button onClick={() => setTab('inbox')}>Inbox</button>
<button onClick={() => setTab('drafts')}>Drafts</button>
<Activity mode={tab === 'inbox' ? 'visible' : 'hidden'}>
<Inbox />
</Activity>
<Activity mode={tab === 'drafts' ? 'visible' : 'hidden'}>
<Drafts />
</Activity>
</>
);
}
When a boundary is hidden:
- its children are hidden with display: none,
- their effects are cleaned up, so subscriptions and timers stop,
- their state and DOM are kept, so a
- they still re-render if their props change, but at a lower priority than what’s visible.
When it becomes visible again, the effects run again and everything is where you left it.
There’s a second use beyond tabs. A hidden Activity can render content the user hasn’t asked for yet, like the next page they’re likely to open, in the background. When they get there, it’s ready.
Two things to watch for
Media keeps playing. The DOM is kept, so a hidden
function VideoTab() {
const ref = useRef(null);
useLayoutEffect(() => {
const video = ref.current;
return () => {
video.pause();
};
}, []);
return <video ref={ref} controls src="/intro.mp4" />;
}
Effects have to survive being stopped and started. Hiding cleans up effects, showing runs them again. This is the same thing Strict Mode has been checking since React 18 with its double mount in development. If your effects already handle that, they’ll handle Activity too.
useEffectEvent: read the latest value without re-running
This one has been in the docs as experimental for a long time, and it’s finally stable. The problem it solves looks like this:
function ChatRoom({ roomId, theme }) {
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.on('connected', () => {
showNotification('Connected!', theme);
});
connection.connect();
return () => connection.disconnect();
}, [roomId, theme]);
}
The effect reads theme, so the lint rule wants it in the dependencies. But then switching between light and dark mode disconnects and reconnects the chat. The connection only depends on roomId. theme is just something we read when the event happens.
useEffectEvent lets you pull that part out:
import { useEffect, useEffectEvent } from 'react';
function ChatRoom({ roomId, theme }) {
const onConnected = useEffectEvent(() => {
showNotification('Connected!', theme);
});
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.on('connected', () => {
onConnected();
});
connection.connect();
return () => connection.disconnect();
}, [roomId]);
}
onConnected always sees the latest theme, like an event handler would. It isn’t a dependency, so the effect only re-runs when roomId changes.
It can also take arguments, which is useful when part of the data should trigger the effect and part shouldn’t:
const onVisit = useEffectEvent((visitedUrl) => {
logVisit(visitedUrl, cartItems.length);
});
useEffect(() => {
onVisit(url);
}, [url]); // log on every page change, not on every cart change
If you’ve used the useLatest ref pattern for this, useEffectEvent replaces it.
The rules
- Don’t put an effect event in a dependency array. Update to the latest eslint-plugin-react-hooks, which knows about it and won’t ask you to.
- Only call it from inside effects, not during render or from regular event handlers.
- Declare it in the same component or hook as the effect that uses it.
- Don’t use it just to make a lint warning go away. If a value should re-run the effect when it changes, it’s a real dependency.
Also in 19.2
A few smaller things worth knowing: React now has its own tracks in the Chrome DevTools Performance panel, showing scheduler priorities and component work. Server-rendered Suspense boundaries are revealed in small batches instead of one by one. And the prefix of useId changed to r, which only matters if you were matching generated ids in tests or CSS.