跪拜 Guibai
← All articles
React.js

React 19.2's <Activity>: 6 Silent Keep-Alive Traps and Their Fixes

By 壹楽 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Multi-tab keep-alive is a core requirement for admin dashboards, and React 19.2 finally makes it native. But these six silent failures cause state loss, broken reloads, and invisible overlays that block clicks unless you apply the documented fixes.

Summary

React 19.2 finally brings a built-in keep-alive mechanism: <Activity> keeps a subtree's DOM and state alive when hidden, with effects cleaned up and re-run on visibility toggles. That capability unlocks multi-tab UIs without third-party libraries, but only if the tab outlet completely replaces <Outlet /> and page components stop reading route hooks. A production implementation passes search and params as props, injects navigation callbacks, and persists view state to the URL so refreshes don't lose anything.

Three traps come from effect and cache semantics. Dependency arrays trigger on re-show, so a ref comparison is required to avoid resetting user data. A global staleTime of 30 seconds makes a reload button a no-op unless the cache is invalidated on remount. And URL-driven overlays portaled to <body> float above hidden tabs, blocking clicks; mounting them into the tab's root container fixes that. Two more traps involve global DOM queries and the non-instantaneous uniqueness of data-visible attributes during switching.

A final trap requires replacing the router outlet entirely: mixing <Outlet /> with <Activity> unmounts the current page on switch. The full solution, including E2E tests, lives in the open-source fastapi-react-admin project. A checklist at the end summarizes every rule, making it easy to apply to your own multi-tab admin.

Takeaways
— Mixing <Outlet /> with <Activity> unmounts the current page; TabOutlet must fully replace the router outlet.
— Background tabs cannot read route hooks; pass search and params as props, and use replace on navigation to avoid history spam.
— Effects that respond to a value change misinterpret re-show as a change; use a ref to compare the last actual value.
— A global staleTime of 30 seconds makes a reload button do nothing unless the cache is invalidated with refetchType: 'active' on remount.
— Hidden tab DOM stays in the document; scope all global queries with data-visible or data-tab attributes, and note that data-visible is not unique for about 18ms in-app and 300ms after refresh.
— URL-driven overlays portaled to <body> float above hidden tabs and block clicks; mount them into the tab's root container via context.
— The reload action must change the page component's key (revision) to force remount, but keep the <Activity> key stable.
Conclusions

The silent nature of these failures is the most dangerous: dependency arrays and query caches assume a component's lifecycle matches its visibility, so they misbehave without errors.

The effect re-run on visibility toggles is a fundamental semantic change; developers must now think of effects as per-visible-cycle, not per-mount.

Display:none is not DOM removal; libraries that query the document globally will still hit hidden nodes, so scoping selectors is a mandatory part of keep-alive.

The 18ms and 300ms windows of dual data-visible values show that keep-alive changes DOM visibility asynchronously, which breaks strict E2E selectors unless routed by tab ID.

Concepts & terms
keep-alive
A pattern that preserves a component's state and DOM when it's hidden, so switching back shows the same input, scroll, and UI state.
React <Activity>
A React 19.2 component that keeps a subtree mounted when hidden using display:none. It preserves DOM and state, runs cleanup on hide, and re-runs effects on show, with updates rendered at lower priority.
From the discussion
Featured comments
组合搭配函数

The point that it's visibility that decides, not unmounting, hits the root. This is how keep-alive should be thought of. After stepping through all six traps one by one and writing it out, this article is worth it.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗