---
name: role-frontend-state-management-b9767c8fbb58
description: |
  State management expertise including local vs global state decisions, Redux Toolkit,
  Zustand, Jotai, MobX, Context API patterns, server state (React Query, SWR) vs
  client state, optimistic updates, and state machines with XState.
allowed-tools: Read, Grep, Glob, Bash
---

# State Management

## When to use
- Deciding where state lives — local, global, server cache, URL, or form
- Setting up Zustand, Jotai, or Redux Toolkit stores
- Replacing axios+useState patterns with TanStack Query
- Implementing optimistic updates for mutations
- Building complex UI flows with XState state machines
- Migrating from Redux to Zustand, or Context to a proper store
- Diagnosing unnecessary re-renders from over-broad state subscriptions

## Core principles
1. **Server state belongs in TanStack Query or SWR** — never duplicate API data in a client store
2. **URL is state too** — pagination, filters, search: use `nuqs` or router search params
3. **Keep it local as long as possible** — global state is a last resort, not a default
4. **Selectors prevent re-renders** — subscribe to slices, not the whole store
5. **XState for impossible states** — if "loading" and "error" can't coexist, the machine enforces it

## Reference Files

- `references/react-state-primitives.md` — state category decision table, useState/useReducer patterns, useContext (when/when not), useSyncExternalStore, React 19 use(), stale closure prevention
- `references/server-state-tanstack-query.md` — TanStack Query v5 queries, optimistic mutations with onMutate/onError/onSettled, infinite queries, prefetch on hover, SWR alternative
- `references/global-state-zustand-jotai-redux.md` — Zustand with immer/persist/devtools middleware, granular selectors, Jotai atom composition, RTK createSlice/createEntityAdapter, URL state with nuqs, React Hook Form + Zod, XState machines, Preact signals, anti-patterns


---
Source attribution: rnavarych
Original project: https://github.com/rnavarych/alpha-engineer/blob/c5b74102c7fd584af3d344ba7c331a7bbec3c729/plugins/roles/role-frontend/skills/state-management/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Raman Navarych

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: rnavarych
Original project: https://github.com/rnavarych/alpha-engineer/blob/c5b74102c7fd584af3d344ba7c331a7bbec3c729/plugins/roles/role-frontend/skills/state-management/SKILL.md
Attribution basis: Source repository record

Fields of work: Design, Development & Engineering, Product Management
Roles: Frontend Developer, Product Manager, UX Designer
Specialties: Cross Platform, Frontend
Tools: Context Api, Jotai, Mobx, React Query, Redux Toolkit, Swr, Xstate, Zustand
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 4085b743f7cf91e756abea8fc92a7de6c939063496106afc9190e7f85dda8fb8
