返回顶部
s

state-management

Deep workflow for client (and hybrid) state—modeling domain vs UI state, server cache vs client store, async and consistency, DevTools, persistence, and testing. Use when choosing Redux/Zustand/Recoil/Context, fixing stale UI, or designing data flow in React/Vue/Svelte apps.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
93
下载量
0
收藏
概述
安装方式
版本历史

state-management

# State Management (Deep Workflow) Most “state bugs” are **ownership** and **lifecycle** bugs: who writes, when it syncs, and what happens on failure. Guide users to **explicit models** instead of ad-hoc globals. ## When to Offer This Workflow **Trigger conditions:** - Prop drilling pain, inconsistent UI, duplicate sources of truth - Stale data after mutations; optimistic UI gone wrong - Choosing a library vs Context vs server-state library (React Query, SWR, Apollo) - SSR/hydration + client state mismatches **Initial offer:** Use **six stages**: (1) inventory state kinds, (2) assign ownership, (3) server vs client boundaries, (4) async & updates, (5) persistence & URLs, (6) testing & DevTools. Confirm **framework** and **data fetching** approach. --- ## Stage 1: Inventory State Kinds **Goal:** Classify **what** state exists before picking tools. ### Categories - **Remote/server state**: API responses, pagination, staleness—often **not** Redux-shaped - **URL state**: filters, tabs, selection when shareable/bookmarkable - **Session UI state**: modals, toggles, transient form drafts - **Client-derived**: sorted/filtered views of remote data—**avoid** storing both raw and derived as writable truths - **Global cross-cutting**: auth user, theme, feature flags—with clear **read-only** vs **mutable** rules **Exit condition:** Table of **state slices** → **source of truth** → **consumers**. --- ## Stage 2: Assign Ownership **Goal:** **One writer** per piece of truth (or strict reducer pattern). ### Rules of Thumb - **Colocate** state where it’s used if not shared—don’t globalize prematurely - **Lift** only when multiple subtrees need it or prop chains hurt **and** ownership is clear - **Avoid** copying server entities into multiple stores without sync rules **Exit condition:** For each slice: **who sets**, **who reads**, **invalidation** story. --- ## Stage 3: Server vs Client Boundaries **Goal:** Prefer **server-state libraries** for remote data; use **client stores** for true client concerns. ### Guidance - **React Query / SWR / RTK Query**: caching, dedupe, refetch, background refresh—use for HTTP JSON - **GraphQL clients**: normalized cache + mutations with **update** policies - **Redux**: great for **predictable** global client state + middleware—not every fetch response ### Anti-patterns - Storing **full API JSON** in Redux without normalization when Apollo/Query would fit - **Double fetch**: SSR then client refetch without coordination—use **hydration** patterns **Exit condition:** Remote data has **defined cache keys**, **stale time**, and **mutation** flow. --- ## Stage 4: Async & Consistency **Goal:** **Loading**, **error**, **empty**, and **retry** are first-class; **optimistic** updates are safe or scoped. ### Practices - **Optimistic UI**: rollback path; idempotent mutations; **server reconciliation** - **Ordering**: serial vs parallel requests; **race** cancellation (AbortController) - **Pagination/infinite scroll**: cursor stability; dedupe pages ### Concurrency - **Last write wins** awareness; versioning or ETags if server supports **Exit condition:** User-visible **failure modes** handled; no silent stale success. --- ## Stage 5: Persistence & URL **Goal:** Decide **what survives refresh**, **what is shareable**, and **security**. ### Options - **URL query** for filters/tabs when users share links - **localStorage/sessionStorage** for drafts—mind **XSS** risk (sensitive data → not localStorage) - **IndexedDB** for offline—complexity tax **Exit condition:** Sensitive data **never** persisted client-side inappropriately. --- ## Stage 6: Testing & DevTools **Goal:** State logic is **unit-testable**; time-travel/debug when needed. ### Practices - **Pure reducers** / small hooks for rules - **Integration tests** for critical flows with **MSW** or mock server - **DevTools**: Redux/Query panels—ensure team knows how to inspect cache --- ## Final Review Checklist - [ ] State classified: remote vs UI vs URL vs derived - [ ] Single owner per truth; invalidation clear - [ ] Server cache strategy chosen for API data - [ ] Async/error/empty/optimistic paths specified - [ ] Persistence/security boundaries respected ## Tips for Effective Guidance - **Start minimal**: Context + server-state library solves many “Redux-sized” problems. - Always ask: “**What is the source of truth** after mutation returns?” - Mention **React Strict Mode** double-invoke when debugging weird effect behavior. ## Handling Deviations - **Legacy Redux everywhere**: incremental migration—**feature modules** first, remote data to Query next. - **Non-React**: map patterns to **signals/stores** (Vue Pinia, Svelte stores) with same ownership discipline.

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 state-management-1776031341 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 state-management-1776031341 技能

通过命令行安装

skillhub install state-management-1776031341

下载 Zip 包

⬇ 下载 state-management v1.0.0

文件大小: 3.08 KB | 发布时间: 2026-4-13 12:10

v1.0.0 最新 2026-4-13 12:10
State-management 1.0.0 – Initial Release

- Introduces a comprehensive workflow for modeling and managing client, server, and hybrid state in modern frontend applications.
- Defines six clear stages: inventory state kinds, assign ownership, clarify boundaries, handle async/consistency, set up persistence/URLs, and ensure testability/dev tool support.
- Includes explicit guidance for choosing between Redux, Context, server-state libraries (React Query, SWR, Apollo), and more.
- Provides detailed checklists, practical anti-patterns, and tips for handling common state missteps and migration from legacy patterns.
- Applicable to React, Vue, and Svelte projects, encouraging explicit source-of-truth models and disciplined ownership.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部