---
title: "How do I keep a component's state when hiding it with React's Activity?"
description: "Learn how React's Activity component hides part of the UI while keeping its state, pauses its Effects, and pre-renders screens the user is likely to open next."
url: https://www.danny.engineering/answer/react/how-do-i-keep-a-components-state-when-hiding-it-with-reacts-activity
author: Danny
published: 2026-11-30T00:00:00.000Z
updated: 2026-11-28T00:09:00.000Z
tags: ["React"]
---

# How do I keep a component's state when hiding it with React's Activity?

## The short answer


Wrap the part of the UI you want to hide in `<Activity>` and switch its `mode` prop between `"visible"` and `"hidden"`. React keeps the component's state and DOM, hides it with `display: none`, and pauses its Effects until it is shown again. `Activity` is stable from React 19.2.


```typescript
import { Activity, useState } from 'react';

function Tabs() {
  const [tab, setTab] = useState<'posts' | 'settings'>('posts');

  return (
    <>
      <TabButtons current={tab} onChange={setTab} />
      <Activity mode={tab === 'posts' ? 'visible' : 'hidden'}>
        <Posts />
      </Activity>
      <Activity mode={tab === 'settings' ? 'visible' : 'hidden'}>
        <Settings />
      </Activity>
    </>
  );
}
```


## Why it happens


The usual way to hide part of a page is conditional rendering, such as `{tab === 'posts' && <Posts />}`. When the condition becomes false, React unmounts the component and throws its state away. Switch back and anything the user typed, any expanded rows and any data held in state are gone.


The old alternative was to keep everything mounted and hide it with CSS. State survives, but the hidden components keep running their Effects (timers, subscriptions, polling) and re-render at full priority whenever their props change, which slows down the screen the user is actually looking at.


`Activity` sits between the two. When `mode` is `"hidden"`, React:

- hides the children with `display: none`,
- keeps their state and their DOM,
- cleans up their Effects, as if they had unmounted,
- renders any updates to them at a low priority, after visible work.

When `mode` goes back to `"visible"`, React re-creates the Effects and shows the saved UI straight away.


## How to fix it


### Replace conditional rendering for screens people return to


Tabs, sidebars, drawers and multi-step forms are the best fit. Swap the `&&` condition for an `Activity` with a `mode`, as in the short answer. The `mode` prop defaults to `"visible"` if you leave it out.


### Pre-render a screen the user is likely to open next


An `Activity` that starts hidden still renders its children, at low priority, but does not run their Effects. You can use this to prepare the next step of a flow in the background:


```typescript
<Activity mode={step === 2 ? 'visible' : 'hidden'}>
  <ShippingForm />
</Activity>
```


If the hidden component reads data through Suspense (for example with `use` and a cached promise, or a framework loader), that data can start loading before the user clicks. Data fetched inside `useEffect` will not load while hidden, because Effects do not run.


### Give your Effects proper cleanup


Hiding runs your cleanup functions, so any Effect without one will leave things running. Some DOM elements also keep working when hidden with `display: none`. A playing video is the classic case, so pause it in a cleanup:


```typescript
function VideoPlayer({ src }: { src: string }) {
  const ref = useRef<HTMLVideoElement>(null);

  useLayoutEffect(() => {
    const video = ref.current;
    return () => {
      video?.pause();
    };
  }, []);

  return <video ref={ref} src={src} controls />;
}
```


## Common pitfalls

- **Expecting hidden Effects to keep running.** A chat subscription inside a hidden `Activity` stops. If it must keep running, move it to a component above the `Activity`.
- **Hiding lots of large trees.** Hidden content still uses memory and DOM nodes. Use `Activity` for a handful of screens, not for every row in a long list.
- **Forgetting it is not a security boundary.** Hidden content is still in the DOM. Do not render data the current user should not see and rely on `Activity` to hide it.
- **Using an older React.** `Activity` shipped as a stable API in React 19.2. Before that it only existed in experimental builds, so check your `react` version if the import fails.

## Related reading

- [React: Activity](https://react.dev/reference/react/Activity)
- [React 19.2 release post](https://react.dev/blog/2025/10/01/react-19-2)
