react-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact 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 unless shared
types.ts - Destructure props in the function signature:
function Card({ title, children }: CardProps) - Avoid barrel files (re-exports) in large projects — they hurt tree-shaking
index.ts
- 每个文件对应一个组件 — 仅当辅助函数是该组件的私有函数时,才将其与组件放在同一文件中
- 优先使用命名导出而非默认导出 — 更利于重构和摇树优化(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 functions when two or more components share it
use* - Dependency arrays — list every reactive value; lint with
react-hooks/exhaustive-deps - /
useCallback— use only when passing to memoized children or expensive computations, not by defaultuseMemo - cleanup — return a cleanup function for subscriptions, timers, and abort controllers
useEffect
- Hooks规则 — 永远不要在条件语句或循环中调用Hooks
- 自定义Hooks — 当两个或多个组件共享相同逻辑时,将可复用逻辑提取到函数中
use* - 依赖数组 — 列出所有响应式值;使用规则进行lint检查
react-hooks/exhaustive-deps - /
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 to mirror state
useEffect - 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>, etc. before reaching for<main><div onClick> - on every
alt— decorative images get<img>alt="" - Keyboard navigation — interactive elements must be focusable and operable via keyboard
- attributes — only when native semantics are insufficient; don't redundantly label
aria-*
- 优先使用语义化HTML — 在使用之前,先使用
<div onClick>、<button>、<a>、<nav>等语义化标签<main> - 所有标签添加
<img>属性 — 装饰性图片使用altalt="" - 键盘导航 — 交互元素必须可聚焦且可通过键盘操作
- 属性 — 仅在原生语义不足时使用;不要重复添加标签
aria-*
Performance
性能
- — wrap pure display components that re-render due to parent changes
React.memo - Lazy loading — use +
React.lazyfor route-level code splittingSuspense - 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 or responsive
next/image; avoid unoptimizedsrcSetin Next.js<img>
- — 包裹因父组件变化而重新渲染的纯展示组件
React.memo - 懒加载 — 使用+
React.lazy实现路由级代码分割Suspense - 列表Key — 使用稳定、唯一的ID;对于可重新排序的列表,永远不要使用数组索引作为Key
- 避免在JSX属性中使用内联对象/数组字面量 — 每次渲染都会创建新的引用
- 图片优化 — 使用或响应式
next/image;在Next.js中避免使用未优化的srcSet<img>
TypeScript Patterns
TypeScript模式
- is optional — prefer plain function declarations with explicit return types
React.FC - — use when the component accepts
PropsWithChildrenbut has no other custom propschildren - Event handlers — type as , not
React.MouseEvent<HTMLButtonElement>any - Generics for reusable components — e.g.,
function List<T>({ items, renderItem }: ListProps<T>) - for config objects — ensures literal types for discriminated unions and enums
as const
- 为可选 — 优先使用带有显式返回类型的普通函数声明
React.FC - — 当组件接受
PropsWithChildren但没有其他自定义Props时使用children - 事件处理函数 — 类型声明为,不要使用
React.MouseEvent<HTMLButtonElement>any - 可复用组件的泛型 — 例如:
function List<T>({ items, renderItem }: ListProps<T>) - 配置对象使用— 确保区分联合类型和枚举的字面量类型
as const
Review Workflow
审查工作流
- Scan recent TSX edits for the patterns above
- Flag any violations with file path and line reference
- Suggest minimal fixes — do not refactor beyond what is needed
- If multiple issues exist in one file, batch them into a single edit
- 扫描最近的TSX编辑内容,检查是否符合上述模式
- 标记违规内容,并附上文件路径和行号引用
- 建议最小化修复方案 — 不要进行超出必要范围的重构
- 如果一个文件中存在多个问题,将它们合并为一次编辑