AgentSkillsCN

react

每当编写、审查或重构 React 代码时,都应使用此技能。包括创建或修改组件、钩子、表单、数据获取、状态管理、路由、测试以及 TypeScript 模式。触发条件包括性能问题(重新渲染、包大小、瀑布效应)、组合模式、effect 反模式、无障碍违规、安全顾虑以及架构决策。也适用于 React 代码审查和对现有 React 代码进行重构以遵循最佳实践。涵盖所有涉及 React 前端代码模式的任务。但不适用于 React Native 移动模式。

SKILL.md
--- frontmatter
name: react
description: "Apply this skill whenever writing, reviewing, or refactoring React code. This includes creating or modifying components, hooks, forms, data fetching, state management, routing, testing, and TypeScript patterns. Triggers for performance issues (re-renders, bundle size, waterfalls), composition patterns, effect anti-patterns, accessibility violations, security concerns, and architectural decisions. Also use for React code reviews and refactoring existing React code to follow best practices. Covers any task involving React frontend code patterns. Do NOT use for React Native mobile patterns."

React Best Practices

Check sibling files for existing patterns first — consistency beats any practice here.

Conventions

Apply to every React file:

  • Component ordering: hooks → state → derived values → event handlers → early returns → render
  • use() replaces useContext() — can be called conditionally (React 19)
  • ref is a regular prop — forwardRef deprecated (React 19)
  • useEffectEvent separates event logic from effect dependencies (React 19.2+)
  • React Compiler auto-memoizes — manual memo/useMemo/useCallback often unnecessary. Follow Rules of React strictly. Use "use no memo" to opt out
  • <Activity mode="visible|hidden"> preserves state for hidden content (tabs, navigation)
  • 0 && <Component /> renders "0" — use count > 0 && or ternary
  • Immutable array methods: .toSorted(), .toReversed(), .toSpliced(), .with()
  • Functional setState: setItems(curr => [...curr, newItem]) — prevents stale closures
  • Module-level app init with let didInit = false guard — not in effects (double-fires in StrictMode)
  • Named exports over default exports — grep-able, refactoring-safe
  • Absolute imports via @/ prefix — never deep ../../../ chains
  • Lazy state initialization: useState(() => expensive()) — not useState(expensive())

References

Read the reference that matches the current task:

  • composition.md — Building components: compound components, variants, boolean props, context interfaces
  • state-management.md — Managing state or writing hooks: colocation, Context, external stores, custom hook patterns
  • effects.md — Deciding whether to use an effect: 12 anti-patterns and alternatives
  • performance.md — Optimizing performance: rendering, bundle size, waterfalls
  • data-fetching.md — Fetching data: TanStack Query/SWR, caching, mutations, optimistic updates
  • forms.md — Building forms: useActionState, useOptimistic, useFormStatus
  • server-rendering.md — Server-side React: RSC, Server Actions, serialization, SSR patterns
  • testing.md — Writing tests: RTL query priority, user-event, MSW, async patterns
  • routing.md — Setting up routes: React Router loaders/actions, lazy routes, error handling
  • styling.md — Styling: Tailwind, CSS Modules, CSS-in-JS, dark mode
  • error-handling.md — Handling errors: react-error-boundary, Suspense integration, granular boundaries
  • security.md — Security review: XSS, URL sanitization, CSP, auth tokens
  • accessibility.md — Accessibility: semantic HTML, ARIA, keyboard, focus management
  • project-structure.md — Project structure: feature-based org, colocation, naming, imports
  • typescript.md — TypeScript types: discriminated unions, generics, utility types, context typing