react-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

React Best-Practices Review

React最佳实践审查

After editing several TSX/JSX files, run through this condensed checklist to catch common issues before they compound.
在编辑多个TSX/JSX文件后,通过这份精简的检查清单排查常见问题,防止问题扩大。

Component Structure

组件结构

  • One component per file — colocate helpers only if they are private to that component
  • Named exports over default exports for better refactoring and tree-shaking
  • Props interface defined inline or colocated, not in a separate
    types.ts
    unless shared
  • Destructure props in the function signature:
    function Card({ title, children }: CardProps)
  • Avoid barrel files (
    index.ts
    re-exports) in large projects — they hurt tree-shaking
  • 每个文件对应一个组件 — 仅当辅助函数是该组件的私有函数时,才将其与组件放在同一文件中
  • 优先使用命名导出而非默认导出 — 更利于重构和摇树优化(tree-shaking)
  • Props接口 — 内联定义或与组件放在同一位置,除非是多个组件共享的类型,否则不要放在单独的
    types.ts
  • 在函数签名中解构Props
    function Card({ title, children }: CardProps)
  • 大型项目中避免桶文件(barrel files)(即
    index.ts
    重导出)—— 它们会影响摇树优化

Hooks

Hooks

  • Rules of Hooks — never call hooks conditionally or inside loops
  • Custom hooks — extract reusable logic into
    use*
    functions when two or more components share it
  • Dependency arrays — list every reactive value; lint with
    react-hooks/exhaustive-deps
  • useCallback
    /
    useMemo
    — use only when passing to memoized children or expensive computations, not by default
  • useEffect
    cleanup
    — return a cleanup function for subscriptions, timers, and abort controllers
  • Hooks规则 — 永远不要在条件语句或循环中调用Hooks
  • 自定义Hooks — 当两个或多个组件共享相同逻辑时,将可复用逻辑提取到
    use*
    函数中
  • 依赖数组 — 列出所有响应式值;使用
    react-hooks/exhaustive-deps
    规则进行lint检查
  • useCallback
    /
    useMemo
    — 仅在传递给已记忆化的子组件或处理昂贵计算时使用,不要默认使用
  • useEffect
    清理
    — 对于订阅、定时器和中止控制器,返回清理函数

State Management

状态管理

  • Colocate state — keep state as close as possible to where it is consumed
  • Derive, don't sync — compute values from existing state instead of adding
    useEffect
    to mirror state
  • Avoid prop drilling past 2–3 levels — use context or composition (render props / children)
  • Server state — use React Query, SWR, or Server Components instead of manual fetch-in-effect
  • 状态就近存放 — 将状态放在离使用它的位置尽可能近的地方
  • 推导而非同步 — 从现有状态计算值,而不是添加
    useEffect
    来镜像状态
  • 避免超过2-3层的属性透传(prop drilling) — 使用Context或组合模式(渲染属性/子组件)
  • 服务端状态 — 使用React Query、SWR或Server Components,而非手动在
    useEffect
    中发起请求

Accessibility (a11y)

可访问性(a11y)

  • Semantic HTML first — use
    <button>
    ,
    <a>
    ,
    <nav>
    ,
    <main>
    , etc. before reaching for
    <div onClick>
  • alt
    on every
    <img>
    — decorative images get
    alt=""
  • Keyboard navigation — interactive elements must be focusable and operable via keyboard
  • aria-*
    attributes
    — only when native semantics are insufficient; don't redundantly label
  • 优先使用语义化HTML — 在使用
    <div onClick>
    之前,先使用
    <button>
    <a>
    <nav>
    <main>
    等语义化标签
  • 所有
    <img>
    标签添加
    alt
    属性
    — 装饰性图片使用
    alt=""
  • 键盘导航 — 交互元素必须可聚焦且可通过键盘操作
  • aria-*
    属性
    — 仅在原生语义不足时使用;不要重复添加标签

Performance

性能

  • React.memo
    — wrap pure display components that re-render due to parent changes
  • Lazy loading — use
    React.lazy
    +
    Suspense
    for route-level code splitting
  • List keys — use stable, unique IDs; never use array index as key for reorderable lists
  • Avoid inline object/array literals in JSX props — they create new references every render
  • Image optimization — use
    next/image
    or responsive
    srcSet
    ; avoid unoptimized
    <img>
    in Next.js
  • React.memo
    — 包裹因父组件变化而重新渲染的纯展示组件
  • 懒加载 — 使用
    React.lazy
    +
    Suspense
    实现路由级代码分割
  • 列表Key — 使用稳定、唯一的ID;对于可重新排序的列表,永远不要使用数组索引作为Key
  • 避免在JSX属性中使用内联对象/数组字面量 — 每次渲染都会创建新的引用
  • 图片优化 — 使用
    next/image
    或响应式
    srcSet
    ;在Next.js中避免使用未优化的
    <img>

TypeScript Patterns

TypeScript模式

  • React.FC
    is optional
    — prefer plain function declarations with explicit return types
  • PropsWithChildren
    — use when the component accepts
    children
    but has no other custom props
  • Event handlers — type as
    React.MouseEvent<HTMLButtonElement>
    , not
    any
  • Generics for reusable components — e.g.,
    function List<T>({ items, renderItem }: ListProps<T>)
  • as const
    for config objects
    — ensures literal types for discriminated unions and enums
  • React.FC
    为可选
    — 优先使用带有显式返回类型的普通函数声明
  • PropsWithChildren
    — 当组件接受
    children
    但没有其他自定义Props时使用
  • 事件处理函数 — 类型声明为
    React.MouseEvent<HTMLButtonElement>
    ,不要使用
    any
  • 可复用组件的泛型 — 例如:
    function List<T>({ items, renderItem }: ListProps<T>)
  • 配置对象使用
    as const
    — 确保区分联合类型和枚举的字面量类型

Review Workflow

审查工作流

  1. Scan recent TSX edits for the patterns above
  2. Flag any violations with file path and line reference
  3. Suggest minimal fixes — do not refactor beyond what is needed
  4. If multiple issues exist in one file, batch them into a single edit
  1. 扫描最近的TSX编辑内容,检查是否符合上述模式
  2. 标记违规内容,并附上文件路径和行号引用
  3. 建议最小化修复方案 — 不要进行超出必要范围的重构
  4. 如果一个文件中存在多个问题,将它们合并为一次编辑