Loading...
Loading...
Compare original and translation side by side
useSyncExternalStoreuseSyncExternalStorecreateWithEqualityFnuse-sync-external-storeuseSyncExternalStoreuseSyncExternalStorecreateWithEqualityFnuse-sync-external-store| Pattern | API | Key Points |
|---|---|---|
| Basic store | | Double parentheses required for TS |
| Atomic selector | | Only re-renders when selected value changes |
| Multiple values | | Import from |
| Persist | | localStorage with SSR hydration handling |
| Devtools | | Redux DevTools integration |
| SubscribeWithSelector | | Subscribe to state slices outside React |
| Middleware order | | Outer to inner wrapping |
| Slices pattern | | Split store by domain |
| SSR provider | | Per-request stores prevent data leaks |
| Immer | | Safe nested state mutations |
| Vanilla store | | Use outside React |
| Reset store | | Use |
| Derived values | | Compute in selector |
| Auto selectors | | Generate |
| 模式 | API | 核心要点 |
|---|---|---|
| 基础存储 | | TypeScript下必须使用双括号 |
| 原子选择器 | | 仅当选中值变化时才会重渲染 |
| 多值选择 | | 从 |
| 持久化 | | 结合localStorage并支持SSR hydration处理 |
| 开发者工具 | | 集成Redux DevTools |
| SubscribeWithSelector | | 在React外部订阅状态切片 |
| 中间件顺序 | | 从外到内包裹 |
| 切片模式 | | 按领域拆分存储 |
| SSR提供者 | | 每个请求独立存储,避免数据泄露 |
| Immer中间件 | | 安全的嵌套状态突变 |
| Vanilla存储 | | 用于非React场景 |
| 重置存储 | | 使用 |
| 派生值 | | 在选择器中计算 |
| 自动生成选择器 | | 自动生成 |
| Mistake | Correct Pattern |
|---|---|
Using | Use |
Selecting full state object with | Use atomic selectors like |
Importing | Import from |
| Creating new object references in selectors causing infinite loops | Use separate primitive selectors or wrap with |
| Using a global singleton store with SSR or Next.js | Use the |
| Tracking initial state manually for reset | Use |
| Using Zustand for server-fetched data that needs caching and revalidation | Use TanStack Query for server state; reserve Zustand for client-only global state |
| 错误 | 正确用法 |
|---|---|
使用单括号的 | TypeScript下请使用 |
使用 | 使用原子选择器如 |
从 | 请从 |
| 在选择器中创建新对象引用导致无限循环 | 使用独立的原始类型选择器,或用 |
| 在SSR或Next.js中使用全局单例存储 | 使用 |
| 手动跟踪初始状态以实现重置 | 使用Zustand自动提供的 |
| 使用Zustand管理需要缓存和重新验证的服务器端获取数据 | 服务器状态请使用TanStack Query;Zustand仅用于客户端专属全局状态 |
PlanTaskExploreTask