React Best Practices

作者 alleneubank2921eb8a685a无许可证52 个星标收录于 2026年10月8日更新于 2026年10月8日仓库3个月前更新

Use when reading or writing React components (.tsx, .jsx files with React imports).

已归档仅含说明Software Development
AI 生成的概览

为 React 组件编写提供规范,涵盖 Effect、ref、自定义 Hook 与组件组合。

功能
该技能提供编写 React 组件的决策框架,核心原则是 Effect 只是用于与外部系统同步的逃生舱。它列出何时该用与不该用 Effect,并给出关于 ref、自定义 Hook、受控与非受控组件、组合、Context 和 flushSync 的指导。它指向一个配套的模式文件以获取代码示例和详细模式。
适用场景
在阅读或编写 React 组件时使用,例如带 React 导入的 .tsx 或 .jsx 文件。它用于在 React 开发中指导关于 Effect、状态、ref 和组件结构的决策。
运行要求
仅为说明文档,不含脚本。该技能说明在处理 React 时应与 typescript-best-practices 技能一同加载,并引用配套的 react-patterns.md 文件。

React Best Practices

Pair with TypeScript

When working with React, always load both this skill and typescript-best-practices together. TypeScript patterns (type-first development, discriminated unions, Zod validation) apply to React code.

Core Principle: Effects Are Escape Hatches

Effects let you "step outside" React to synchronize with external systems. Most component logic should NOT use Effects. Before writing an Effect, ask: "Is there a way to do this without an Effect?"

Decision Tree

  1. Need to respond to user interaction? Use event handler
  2. Need computed value from props/state? Calculate during render
  3. Need cached expensive calculation? Use useMemo
  4. Need to reset state on prop change? Use key prop
  5. Need to synchronize with external system? Use Effect with cleanup
  6. Need non-reactive code in Effect? Use useEffectEvent
  7. Need mutable value that doesn't trigger render? Use ref

When to Use Effects

Synchronizing with external systems: browser APIs (WebSocket, IntersectionObserver), third-party non-React libraries, window/document event listeners, non-React DOM elements (video, maps).

When NOT to Use Effects

  • Derived state — calculate during render
  • Expensive calculations — use useMemo
  • Resetting state on prop change — use key prop
  • Responding to user events — use event handlers
  • Notifying parent of state changes — update both in the same event handler
  • Chains of effects — calculate derived state and update in one event handler

Refs

  • Use for values that don't affect rendering (timer IDs, DOM node references)
  • Never read or write ref.current during render; only in event handlers and effects
  • Use ref callbacks (not useRef in loops) for dynamic lists
  • Use useImperativeHandle to limit what parent can access

Custom Hooks

  • Share logic, not state — each call gets an independent state instance
  • Name useXxx only if it actually calls other hooks; otherwise use a regular function
  • Avoid lifecycle hooks (useMount, useEffectOnce) — use useEffect directly so the linter catches missing deps
  • Keep focused on a single concrete use case

Component Patterns

  • Controlled: parent owns state; uncontrolled: component owns state
  • Prefer composition with children over prop drilling
  • Treat boolean props that switch large component trees (isEditing, isThread, hideAttachments) as a composition smell; prefer separate composed components for distinct use cases
  • For complex reusable UI, prefer compound components with provider-scoped state/actions over monolithic components with many optional props
  • Use Context for scoped component families as well as truly global state, when it defines a local interface consumed by descendants
  • Render JSX directly for UI variation; avoid config-array mini-frameworks unless the config is real domain data
  • Lift the provider boundary when sibling or external controls need access to the same state/actions
  • Use flushSync when you need to read the DOM synchronously after a state update

See react-patterns.md for code examples and detailed patterns.

来源与署名

来源:alleneubank/claude-code位于.claude/skills/react-best-practices提交2921eb8

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架