# `<Activity>`

> The `<Activity>` component in React hides and restores parts of the UI while preserving their state.

## How do I hide a React component without losing its state?

The component that hides its children with the 'hidden' mode and shows them with 'visible', preserving their state and DOM. While hidden, Effects are cleaned up and updates get a lower priority. Useful for tabs and pre-rendering.

- Topic: Components
- Type: component

## Example

Wrong:

```jsx
{tab === 'drafts' && <Drafts />}
{tab === 'sent' && <Sent />}
```

Right:

```jsx
<Activity mode={tab === 'drafts' ? 'visible' : 'hidden'}>
  <Drafts />
</Activity>
<Activity mode={tab === 'sent' ? 'visible' : 'hidden'}>
  <Sent />
</Activity>
```

Hidden tabs keep their state and DOM instead of unmounting, so a half-typed draft is still there when the user comes back.

## Common mistakes

- A video keeps playing in a hidden tab because no cleanup pauses it.

## Key points

- Hidden UI uses `<Activity mode='hidden'>` instead of `{show && <Panel />}` when its state must survive.
- Effects in the hidden tree have cleanups, since hiding runs them like an unmount.
- Media and timers are paused in effect cleanup, because the DOM stays alive while hidden.

## Interview questions

### What happens to Effects inside a hidden Activity?

React runs their cleanups when the tree is hidden and runs their setups again when it becomes visible.

### Activity vs CSS display none: what is the difference?

Both hide the DOM, but `<Activity>` also cleans up Effects and gives hidden updates low priority, so hidden UI does not compete with visible UI.

### Does a hidden Activity still re-render?

Yes, but at a lower priority than visible content. This lets React prepare hidden UI in the background without slowing down the page.

## Related cards

- [States](https://wonderium.dev/react/states.md)
- [`<Suspense>`](https://wonderium.dev/react/suspense.md)
- [useEffect()](https://wonderium.dev/react/use-effect.md)
- [useTransition()](https://wonderium.dev/react/use-transition.md)
- [Component Lifecycle](https://wonderium.dev/react/component-lifecycle.md)

---

- Level: [React](https://wonderium.dev/react.md)
- HTML version: https://wonderium.dev/react/activity
- Last updated: 2026-11-25
