react-19
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact 19.2 Core Features
React 19.2 核心特性
Agent Workflow (MANDATORY)
Agent 工作流(强制执行)
Before ANY implementation, use to spawn 3 agents:
TeamCreate- fuse-ai-pilot:explore-codebase - Analyze existing React patterns and component structure
- fuse-ai-pilot:research-expert - Verify latest React 19.2 docs via Context7/Exa
- mcp__context7__query-docs - Check use(), useOptimistic, useActionState, Activity patterns
After implementation, run fuse-ai-pilot:sniper for validation.
在进行任何实现工作之前,使用生成3个Agent:
TeamCreate- fuse-ai-pilot:explore-codebase - 分析现有React模式和组件结构
- fuse-ai-pilot:research-expert - 通过Context7/Exa验证最新的React 19.2文档
- mcp__context7__query-docs - 检查use()、useOptimistic、useActionState、Activity的使用模式
实现完成后,运行fuse-ai-pilot:sniper进行验证。
What's New in React 19.2
React 19.2 新特性
New Hooks
新Hooks
| Hook | Purpose | Guide |
|---|---|---|
| Read promises/context in render | |
| Optimistic UI updates | |
| Form action state management | |
| Form pending state (child components) | |
| Non-reactive callbacks in effects | |
→ See for detailed usage
references/new-hooks.md| Hook | 用途 | 指南 |
|---|---|---|
| 在渲染阶段读取Promise/上下文 | |
| 实现乐观UI更新 | |
| 表单动作状态管理 | |
| 表单加载状态(子组件中使用) | |
| 副作用中的非响应式回调 | |
→ 详细用法请参阅
references/new-hooks.mdClassic Hooks (React 18+)
经典Hooks(React 18+)
State Hooks
状态Hooks
| Hook | Purpose | Guide |
|---|---|---|
| Local component state | |
→ For global state, see skill
react-state| Hook | 用途 | 指南 |
|---|---|---|
| 组件本地状态管理 | |
→ 全局状态管理请参阅技能文档
react-stateEffect Hooks
副作用Hooks
| Hook | Purpose | Guide |
|---|---|---|
| Side effects after paint | |
| Sync DOM before paint | |
| Hook | 用途 | 指南 |
|---|---|---|
| 渲染完成后执行副作用 | |
| 渲染前同步DOM操作 | |
Ref Hooks
引用Hooks
| Hook | Purpose | Guide |
|---|---|---|
| DOM access, mutable values | |
| Customize ref API | |
| Hook | 用途 | 指南 |
|---|---|---|
| DOM访问与可变值存储 | |
| 自定义Ref API | |
Performance Hooks (Rare with Compiler)
性能优化Hooks(使用Compiler后基本无需)
| Hook | Purpose | Guide |
|---|---|---|
| Memoize expensive values | |
| Memoize functions | |
→ React Compiler handles most memoization automatically
| Hook | 用途 | 指南 |
|---|---|---|
| 缓存计算开销大的值 | |
| 缓存函数 | |
→ React Compiler会自动处理大部分缓存逻辑
Other Hooks
其他Hooks
| Hook | Purpose | Guide |
|---|---|---|
| Unique IDs for accessibility | |
| External store subscription | |
| Hook | 用途 | 指南 |
|---|---|---|
| 生成无障碍访问所需的唯一ID | |
| 外部状态库订阅 | |
Custom Hooks
自定义Hooks
→ See for patterns
→ See for implementations
references/custom-hooks-patterns.mdreferences/templates/custom-hooks.md→ 实现模式请参阅
→ 代码实现请参阅
references/custom-hooks-patterns.mdreferences/templates/custom-hooks.mdActivity Component (19.2)
Activity 组件(19.2版本新增)
Hide/show components while preserving state:
typescript
<Activity mode={isActive ? 'visible' : 'hidden'}>
<TabContent />
</Activity>→ See for patterns
references/activity-component.md在隐藏/显示组件时保留其状态:
typescript
<Activity mode={isActive ? 'visible' : 'hidden'}>
<TabContent />
</Activity>→ 使用模式请参阅
references/activity-component.mdReact Compiler (19.1+)
React Compiler(19.1版本及以上)
Automatic memoization - useMemo/useCallback mostly obsolete:
- Build-time optimization
- No more manual memoization in most cases
- 2.5× faster interactions reported
→ See for details
references/react-compiler.md自动缓存优化 - useMemo/useCallback基本过时:
- 构建阶段优化
- 大多数场景下无需手动缓存
- 据报告交互速度提升2.5倍
→ 详细信息请参阅
references/react-compiler.mdQuick Reference
快速参考
use() Hook
use() Hook
typescript
// Read promise in render (with Suspense)
const data = use(dataPromise)
// Read context conditionally (unique to use())
if (condition) {
const theme = use(ThemeContext)
}→ See
references/templates/use-promise.mdtypescript
// 在渲染阶段读取Promise(配合Suspense使用)
const data = use(dataPromise)
// 条件式读取上下文(use()独有的特性)
if (condition) {
const theme = use(ThemeContext)
}→ 示例请参阅
references/templates/use-promise.mduseOptimistic
useOptimistic
typescript
const [optimisticValue, setOptimistic] = useOptimistic(actualValue)
// Update UI immediately, server updates later→ See
references/templates/optimistic-update.mdtypescript
const [optimisticValue, setOptimistic] = useOptimistic(actualValue)
// 立即更新UI,后续同步服务器数据→ 示例请参阅
references/templates/optimistic-update.mduseActionState
useActionState
typescript
const [state, action, isPending] = useActionState(asyncFn, initialState)→ See
references/templates/action-form.mdtypescript
const [state, action, isPending] = useActionState(asyncFn, initialState)→ 示例请参阅
references/templates/action-form.mduseEffectEvent (19.2)
useEffectEvent (19.2)
typescript
const onEvent = useEffectEvent(() => {
// Always has fresh props/state, doesn't trigger re-run
})
useEffect(() => {
connection.on('event', onEvent)
}, []) // No need to add onEvent to deps→ See
references/new-hooks.mdtypescript
const onEvent = useEffectEvent(() => {
// 始终获取最新的props/state,不会触发副作用重跑
})
useEffect(() => {
connection.on('event', onEvent)
}, []) // 无需将onEvent加入依赖数组→ 详细用法请参阅
references/new-hooks.mdBreaking Changes from 18
从React 18升级的破坏性变更
| Change | Migration |
|---|---|
| Remove |
| Use |
| Import from |
→ See
references/migration-18-19.md| 变更内容 | 迁移方案 |
|---|---|
| 移除 |
| 直接使用 |
| 从 |
→ 迁移指南请参阅
references/migration-18-19.mdBest Practices
最佳实践
- Data fetching: Use + Suspense, NOT useEffect
use() - Forms: Use Actions + useActionState
- Optimistic UI: Use for instant feedback
useOptimistic - Tabs/Modals: Use to preserve state
<Activity> - Effect events: Use for non-reactive callbacks
useEffectEvent - Memoization: Let React Compiler handle it
- 数据获取:使用+ Suspense,而非useEffect
use() - 表单处理:使用Actions + useActionState
- 乐观UI:使用实现即时反馈
useOptimistic - 标签页/模态框:使用组件保留状态
<Activity> - 副作用事件:使用处理非响应式回调
useEffectEvent - 缓存优化:交由React Compiler自动处理
Templates
模板参考
React 19 Patterns
React 19 开发模式
| Template | Use Case |
|---|---|
| Form with useActionState |
| useOptimistic pattern |
| Activity component tabs |
| use() with promises |
| 模板 | 使用场景 |
|---|---|
| 基于useActionState的表单实现 |
| useOptimistic 实现模式 |
| Activity组件实现标签页 |
| use()配合Promise的实现 |
Classic Hooks Patterns
经典Hooks开发模式
| Template | Use Case |
|---|---|
| useState patterns |
| useEffect patterns |
| useRef patterns |
| Custom hooks implementations |
| useSyncExternalStore patterns |
| 模板 | 使用场景 |
|---|---|
| useState 实现模式 |
| useEffect 实现模式 |
| useRef 实现模式 |
| 自定义Hooks实现 |
| useSyncExternalStore 实现模式 |
Performance Patterns
性能优化模式
| Template | Use Case |
|---|---|
| TanStack Virtual for long lists |
| Code splitting and lazy loading |
| React DevTools Profiler |
| 模板 | 使用场景 |
|---|---|
| 使用TanStack Virtual实现长列表 |
| 代码分割与懒加载 |
| React DevTools 性能分析 |
Performance
性能优化建议
Virtualization
虚拟列表渲染
Render only visible items for large lists (100+ items).
→ See
references/virtualization.md对于大型列表(100条以上数据),仅渲染可见区域的元素。
→ 详细指南请参阅
references/virtualization.mdLazy Loading
懒加载
Code split routes and heavy components for smaller bundles.
→ See
references/lazy-loading.md对路由和重型组件进行代码分割,减小打包体积。
→ 详细指南请参阅
references/lazy-loading.mdProfiling
性能分析
Measure render performance with DevTools Profiler.
→ See
references/profiling.mdNote: With React Compiler (19.1+), manual memo/useMemo/useCallback optimizations are mostly obsolete. Profile first to verify if optimization is needed.
使用DevTools Profiler测量渲染性能。
→ 详细指南请参阅
references/profiling.md注意:在使用React Compiler(19.1+)后,手动使用useMemo/useCallback进行缓存优化的场景基本消失。请先进行性能分析,再决定是否需要优化。
Forbidden (Outdated Patterns)
禁用(过时模式)
- ❌ for data fetching → use
useEffect+ Suspenseuse() - ❌ → use
forwardRefas propref - ❌ → use
<Context.Provider><Context value={}> - ❌ Manual useMemo/useCallback everywhere → let Compiler handle it
- ❌ Conditional rendering for state preservation → use
<Activity>
- ❌ 使用进行数据获取 → 改用
useEffect+ Suspenseuse() - ❌ 使用→ 直接将
forwardRef作为属性传递ref - ❌ 使用→ 直接使用
<Context.Provider><Context value={}> - ❌ 到处手动使用useMemo/useCallback → 交由Compiler自动处理
- ❌ 使用条件渲染保留组件状态 → 改用组件
<Activity>