React best practices

From akillness/jeo-skills · View interactive version

React best practices is an AI agent skill from the GitHub repository akillness/jeo-skills. Run measurement-led React and Next.js performance audits for waterfalls, bundle size, RSC/server-client boundaries, hydration mismatch, rerender churn, script cost, and slow page interactions. Use when the user needs help diagnosing or refactoring a slow React UI, App Router route, heavy client comp

How to use this skill

Skill file

Vercel React Best Practices

Measurement-led React and Next.js performance skill for routing slow-page work into the right packet before refactoring. The canonical focus is waterfalls, bundle weight, RSC or client-boundary mistakes, hydration/script cost, and rerender churn; the deep AGENTS.md remains available, but day-to-day use should start from the lighter reference bundle and route-outs.

When to use this skill

Reference these guidelines when:

Rule Categories by Priority

| Priority | Category | Impact | Prefix |

|----------|----------|--------|--------|

| 1 | Eliminating Waterfalls | CRITICAL | async- |

| 2 | Bundle Size Optimization | CRITICAL | bundle- |

| 3 | Server-Side Performance | HIGH | server- |

| 4 | Client-Side Data Fetching | MEDIUM-HIGH | client- |

| 5 | Re-render Optimization | MEDIUM | rerender- |

| 6 | Rendering Performance | MEDIUM | rendering- |

| 7 | JavaScript Performance | LOW-MEDIUM | js- |

| 8 | Advanced Patterns | LOW | advanced- |

Quick Reference

1. Eliminating Waterfalls (CRITICAL)

2. Bundle Size Optimization (CRITICAL)

3. Server-Side Performance (HIGH)

4. Client-Side Data Fetching (MEDIUM-HIGH)

5. Re-render Optimization (MEDIUM)

6. Rendering Performance (MEDIUM)

7. JavaScript Performance (LOW-MEDIUM)

8. Advanced Patterns (LOW)

Instructions

Step 1: Classify the primary bottleneck before suggesting fixes

Start with references/perf-triage-modes.md and put the task into one dominant packet:

Step 2: Use the smallest support packet that answers the question

Step 3: Prefer measurement-led fixes over blanket cargo culting

Use the measurement checklist before recommending broad memoization or cache-heavy rewrites. React Compiler, bundle analyzers, and web vitals instrumentation have changed which old React performance heuristics are still worth applying blindly.

Step 4: Prioritize high-impact fixes first

  1. Remove waterfalls and late-started async work.
  2. Cut route JS and heavy client boundaries.
  3. Fix hydration or script-placement mistakes.
  4. Address rerender churn once the larger loading and boundary costs are understood.

Step 5: Route adjacent work out instead of absorbing everything

Examples

Promise.all for Independent Operations (CRITICAL)

// ❌ Sequential: 3 round trips
const user = await fetchUser()
const posts = await fetchPosts()
const comments = await fetchComments()

// ✅ Parallel: 1 round trip
const [user, posts, comments] = await Promise.all([
  fetchUser(),
  fetchPosts(),
  fetchComments()
])

Avoid Barrel File Imports (CRITICAL)

// ❌ Imports entire library (200-800ms import cost)
import { Check, X, Menu } from 'lucide-react'

// ✅ Imports only what you need
import Check from 'lucide-react/dist/esm/icons/check'
import X from 'lucide-react/dist/esm/icons/x'

Dynamic Imports for Heavy Components (CRITICAL)

// ❌ Monaco bundles with main chunk ~300KB
import { MonacoEditor } from 'monaco-editor-panel'

// ✅ Monaco loads on demand
import dynamic from 'next/dynamic'
const MonacoEditor = dynamic(
  () => import('monaco-editor-panel').then(m => m.MonacoEditor),
  { ssr: false }
)

Use Functional setState (MEDIUM)

// ❌ Requires state as dependency, stale closure risk
const addItems = useCallback((newItems) => {
  setItems([...items, ...newItems])
}, [items])

// ✅ Stable callback, no stale closures
const addItems = useCallback((newItems) => {
  setItems(curr => [...curr, ...newItems])
}, [])

Best practices

  1. Start with the lightest support packet that matches the problem; use AGENTS.md only when the smaller references are not enough.
  2. Prefer evidence-led fixes: React Profiler, bundle analyzers, and Web Vitals should shape the recommendation before you prescribe memoization or caching.
  3. Treat React Compiler as a live constraint on old memoization folklore; do not default to “wrap everything in memo”.
  4. Keep React-specific advice separate from broader frontend concerns: use state-management for app-wide state choices, web-accessibility for WCAG remediation, and performance-optimization for non-React-wide bottlenecks.
  5. Escalate to the compatibility alias vercel-react-best-practices only when an existing workflow explicitly requires that exact name.
  6. Keep PR-budget and bundle-diff workflows in mind for real team usage; the skill should help with operational audits, not just code-level micro-fixes.

Constraints

Required Rules (MUST)

  1. Eliminate waterfalls: Use Promise.all, Suspense
  2. Bundle optimization: Prohibit barrel imports, use dynamic imports
  3. RSC boundaries: Serialize only the data you need

Prohibited (MUST NOT)

  1. Sequential await: Do not run independent fetches sequentially
  2. Array mutations: Use toSorted() instead of sort()
  3. Inline objects in React.cache: Causes cache misses

References

Metadata

Version

Related Skills

Tags

#React #Next.js #performance #optimization #vercel #waterfalls #bundle-size #RSC #frontend

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.