agentleFS
LeonxLJXLeonxLJX/web-engineer-skills/skills/state-management/SKILL.md

state-management

Skill0 starsChanged 2 months agoMITView on GitHub

Choose and wire state correctly — server cache vs client state, where state lives, colocation, undo/redo, and URL as state. Use when an app's data flow is getting tangled or before adding another state library.

27 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use error-handling, i18n and 9 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • MicrosoftSkill

    error-handling

    统一错误处理系统。在添加 API 端点、修改错误处理、添加前端 API 调用、编写错误相关测试时使用。

    Also in 6 projects that use this

  • lbednerSkill

    i18n

    Use when a code or template change introduces a new translation message key, in either the framework CLI (aegis/i18n/locales/) or a generated project's CLI (the app/i18n/locales/ template tree). Covers adding the real English string, stubbing the same key into every other locale for parity, and the separate translation PR that later replaces placeholders with reviewed native text.

    Also in 4 projects that use this

  • mijunriSkill

    database

    数据库脚本与操作规范。脚本放在 db_scripts 目录,遵循查询优化、事务、安全等实践。

    Also in 3 projects that use this

  • genomewalkerSkill

    resume

    Resume a thread by loading its ~800-token context capsule

    Also in 3 projects that use this

  • NVIDIASkill

    testing

    Run Python and Rust tests for physicsnemo-curator using uv + pytest with coverage reporting via pytest-cov, and cargo-nextest for Rust tests. Includes benchmark workflows with pytest-benchmark and criterion.

    Also in 6 projects that use this

  • 0xSeroSkill

    security

    Also in 4 projects that use this

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 21 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Only model it where the product needs it (editors, canvases): keep a past/present/future triple of the *document* state, commit at …

    line 57
  2. Distinctive:

    Prefer signals/atoms-style libs (Zustand, Jotai, Pinia) over single-tree flux for new code.

    line 61

“Common” is measured: kept by at least 6 in 10 of the 21 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

State management: put each piece where it belongs

The only taxonomy that matters

KindExamplesHome
Server stateusers, orders, search resultsdata-fetching cache (TanStack Query / SWR / RSC)
Client statemodal open, form draft, selectionuseState/store, colocated
URL statepage, filters, tab, sortthe URL (search params)
Global UItheme, auth sessiontiny store + persistence

Most "we need Redux" moments are really "our fetch cache and our UI state got tangled".

Server state ≠ client state

Server state has properties no client store models well: caching, invalidation, retries, race conditions, optimistic updates. Don't hand-roll these:

// ❌ hand-rolled cache
const [users, setUsers] = useState<User[]>([])
useEffect(() => { fetchUsers().then(setUsers) }, [])

// ✅ the cache handles dedupe/retry/race/invalidation
const { data: users } = useQuery({ queryKey: ['users'], queryFn: fetchUsers })

Rules:

  1. Mutations invalidate keys, they don't patch arrays.
  2. Optimistic update = apply → rollback on error → always reconcile with server response.
  3. Track updatedAt; stale-while-revalidate is a feature, not a bug.

Colocate before you centralize

Lift state only when a second consumer appears. State used by one component lives in that component. A "global store" with 40% single-consumer keys is a symptom.

URL is state (the most forgotten one)

Filters, tabs, pagination, sort → search params. You get shareability, back button, and SSR for free; refresh preserves context.

const [params, setParams] = useSearchParams()
setParams({ tab: 'advanced', page: '2' })   // not useState('advanced')

Forms are their own domain

Controlled fields in local state (or a form library); never push keystrokes into a global store. Validation mirrors the server's contract; one source of truth for error shape.

Undo/redo

Only model it where the product needs it (editors, canvases): keep a past/present/future triple of the document state, commit at meaningful actions (not keystrokes), cap history depth.

When you do need a store

  • Prefer signals/atoms-style libs (Zustand, Jotai, Pinia) over single-tree flux for new code.
  • Selectors over subscriptions: components subscribe to slices, not the store; derived values are selectors/computed, never copied.
  • One direction: actions mutate, components read. A store that both components write and components derive from in cycles is a design bug.

Definition of done (agent checklist)

  • Server state lives in a query cache, not useEffect+setState
  • Shareable view state (filters/tabs/page) is in the URL
  • State colocated; store has no single-consumer keys
  • No derived state kept in sync by effects
  • Mutations invalidate; optimistic paths roll back

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.