| 1 | --- |
| 2 | title: Use Activity Component for Show/Hide |
| 3 | impact: MEDIUM |
| 4 | impactDescription: preserves state/DOM |
| 5 | tags: rendering, activity, visibility, state-preservation |
| 6 | --- |
| 7 | |
| 8 | ## Use Activity Component for Show/Hide |
| 9 | |
| 10 | Use React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility. |
| 11 | |
| 12 | **Usage:** |
| 13 | |
| 14 | ```tsx |
| 15 | import { Activity } from 'react' |
| 16 | |
| 17 | function Dropdown({ isOpen }: Props) { |
| 18 | return ( |
| 19 | <Activity mode={isOpen ? 'visible' : 'hidden'}> |
| 20 | <ExpensiveMenu /> |
| 21 | </Activity> |
| 22 | ) |
| 23 | } |
| 24 | ``` |
| 25 | |
| 26 | Avoids expensive re-renders and state loss. |
| 27 |