laguagu/claude-code-nextjs-skills / skills/react-best-practices
react-best-practices
VerifiedReact and Next.js performance optimization guide from Vercel Engineering (70 rules in 8 categories). Use when writing, reviewing, or refactoring React/Next.js code to eliminate waterfalls, reduce bundle size, prevent re-renders, optimize Server Components, or fix performance bottlenecks. Triggers on data fetching, Promise.all, Suspense, useMemo, dynamic imports, bundle analysis, code-splitting, and rendering performance tasks.
rules/rendering-activity.md
View on GitHub---
title: Use Activity Component for Show/Hide
impact: MEDIUM
impactDescription: preserves state/DOM without re-mount cost
tags: rendering, activity, visibility, state-preservation
requires: React 19.2+
---
## Use Activity Component for Show/Hide
Use React's `<Activity>` to preserve state/DOM for expensive components that
frequently toggle visibility. `<Activity>` is stable since React 19.2.
**Why not `{show && <Component />}`?**
- Unmounts the subtree → loses component state
- Re-runs effects on every mount
- Rebuilds DOM on every show
**Why not `display: none`?**
- Effects still run (setInterval, fetch, subscriptions waste resources)
- Doesn't signal React that the subtree is inactive
**Incorrect (loses state and re-runs effects):**
```tsx
function App() {
const [tab, setTab] = useState<'a' | 'b'>('a')
return tab === 'a' ? <ExpensiveChartA /> : <ExpensiveChartB />
// Switching tabs unmounts — scroll position, form state, subscriptions all reset
}
```
**Correct (preserves state, pauses effects when hidden):**
```tsx
import { Activity } from 'react'
function App() {
const [tab, setTab] = useState<'a' | 'b'>('a')
return (
<>
<Activity mode={tab === 'a' ? 'visible' : 'hidden'}>
<ExpensiveChartA />
</Activity>
<Activity mode={tab === 'b' ? 'visible' : 'hidden'}>
<ExpensiveChartB />
</Activity>
</>
)
}
```
**Behavior of `hidden` mode:**
- Children render to DOM but are not displayed
- Effects cleanup when mode switches to `'hidden'`
- Effects re-run when mode switches back to `'visible'`
- State (`useState`, refs, uncontrolled inputs) is preserved across toggles
**When to use:**
- Tab panels where users return to the same tab
- Modal/drawer content that's expensive to mount
- Off-screen navigation views in mobile apps
- Any subtree where you want to pay mount cost once
**When NOT to use:**
- Single-use content (e.g. one-time tooltips)
- Very large subtrees where accumulated DOM becomes a memory problem
- Lists where entries come and go permanently
Reference: [React docs: `<Activity>`](https://react.dev/reference/react/Activity)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |