Loading...
Loading...
Compare original and translation side by side
| Pattern | API / Approach | Key Points |
|---|---|---|
| Data fetching | | Replaces useEffect+useState fetch pattern |
| Form handling | | Built-in pending states and error handling |
| Optimistic UI | | Instant feedback while server processes |
| Non-urgent updates | | Mark updates as interruptible |
| Effect events | | Reactive values without re-triggering effects |
| Form pending status | | Read parent form pending state from child component |
| Unique IDs | | Hydration-safe IDs for accessibility |
| Server state | React Query / | Caching, deduplication, background refetch |
| Client state (local) | | Single component or transient values |
| Client state (global) | Zustand / Context | Cross-component client-only state |
| Derived state | Compute during render | Never sync derived values with effects |
| Lazy initialization | | Avoid eager computation on every render |
| Component types | Page, Feature, UI | Route entry, business logic, reusable primitives |
| Memoization | Trust React Compiler first | Manual useMemo/useCallback only when needed |
| Ref as prop | | No |
| Ref cleanup | Return function from ref callback | Cleanup runs on detach instead of |
| Code splitting | | Lazy-load heavy components |
| Parallel fetches | | Eliminate sequential await waterfalls |
| Request dedup | | Per-request server-side deduplication |
| Abort server work | | Cancel expensive async work when client disconnects |
| State preservation | | Hide UI while keeping state mounted |
| 模式 | API / 实现方式 | 核心要点 |
|---|---|---|
| 数据获取 | | 替代useEffect+useState的获取模式 |
| 表单处理 | | 内置等待状态与错误处理 |
| 乐观UI | | 服务器处理期间提供即时反馈 |
| 非紧急更新 | | 将更新标记为可中断 |
| 副作用事件 | | 无需重新触发副作用即可访问响应式值 |
| 表单等待状态 | | 从子组件读取父表单的等待状态 |
| 唯一ID | | 适用于可访问性的 hydration 安全ID |
| 服务端状态 | React Query / | 缓存、去重、后台重新获取 |
| 客户端状态(本地) | | 单个组件或临时值 |
| 客户端状态(全局) | Zustand / Context | 跨组件的客户端专属状态 |
| 派生状态 | 渲染期间计算 | 绝不要使用副作用同步派生值 |
| 延迟初始化 | | 避免在每次渲染时执行昂贵计算 |
| 组件类型 | 页面、功能、UI | 路由入口、业务逻辑、可复用基础组件 |
| 记忆化 | 优先信任React Compiler | 仅在必要时手动使用useMemo/useCallback |
| 作为属性的Ref | 函数组件上的 | React 19中无需 |
| Ref清理 | 从Ref回调中返回函数 | 清理操作在组件卸载时运行,而非调用 |
| 代码拆分 | | 懒加载大型组件 |
| 并行获取 | | 消除顺序await导致的瀑布流 |
| 请求去重 | | 每请求的服务端去重 |
| 终止服务端工作 | | 客户端断开连接时取消昂贵的异步工作 |
| 状态保留 | | 隐藏UI的同时保持状态挂载 |
| Mistake | Correct Pattern |
|---|---|
| Fetching data in useEffect with useState | Use the |
| Storing derived values in state and syncing with effects | Compute derived values during render; never use effects for state synchronization |
| Wrapping everything in useMemo and useCallback | Trust React Compiler first; only add manual memoization for expensive computations or memoized children |
Using | Use |
Using | Use ternary |
| Using Math.random() or Date for IDs | Use |
| Putting reactive values in effect deps to read latest value | Use |
| Creating object literals as effect dependencies | Hoist static objects outside the component or use primitive dependencies |
Using | Pass |
| Mutating props or state during render | Follow Rules of React for React Compiler compatibility: pure renders, no side effects |
| 错误 | 正确模式 |
|---|---|
| 在useEffect中结合useState获取数据 | 使用 |
| 将派生值存储在状态中并通过副作用同步 | 在渲染期间计算派生值;绝不要使用副作用进行状态同步 |
| 给所有内容包裹useMemo和useCallback | 优先信任React Compiler;仅在处理昂贵计算或记忆化子组件时添加手动记忆化 |
使用会改变状态的 | 使用 |
使用 | 使用三元表达式 |
| 使用Math.random()或Date生成ID | 使用 |
| 在副作用依赖中放入响应式值以读取最新值 | 使用 |
| 将对象字面量作为副作用依赖 | 将静态对象提升到组件外部,或使用原始类型依赖 |
在React 19项目中使用 | 直接将 |
| 渲染期间修改属性或状态 | 遵循React规则以兼容React Compiler:纯渲染、无副作用 |
ExploreTaskPlanExploreTaskPlan