react-state-management
Skill1 starsChanged 2 months agoMITView on GitHub
Decide where each piece of React state should live and stop the rework cycles it causes - server state via a query library, local UI state with useState, genuinely global client state in a small store, shareable state in the URL, and forms in a form library. Covers the anti-patterns that dominate code review: mirroring props into state, context re-render storms, and using a global store for what is really a URL param or a server field. Use when someone asks "should this be in context or zustand or redux", when a provider re-renders the whole app, when state is duplicated in two places, or when debugging "stale state" / "lost scroll position" / "back button does not work". Triggers on "lifting state up", "prop drilling", "global state", "zustand", "redux toolkit", "context provider", "URL as state", "server state", "useReducer vs useState", "too many re-renders".
18 other projects use a version of this.
Adopted by
Most recently taken up by
- Rain-kl/OpenFlaretheir own version, first seen Nov 30, 2026
- gromov0737/skillstheir own version, first seen Nov 29, 2026
- AtilaMedeiros/skillstheir own version, first seen Nov 16, 2026
- xfstudio/skillstheir own version, first seen Oct 13, 2026
- Samueladewole/claude-skillstheir own version, first seen Oct 13, 2026
- involvex/youtube-music-clitheir own version, first seen Oct 8, 2026
Used with
Projects with it also use python-project-structure, postgresql-table-design and 1 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.
Aver005Skillpython-project-structure
Python project organization, module architecture, and public API design. Use when setting up new projects, organizing modules, defining public interfaces with __all__, or planning directory layouts.
Also in 3 projects that use this
nilecuiSkillpostgresql-table-design
Design a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features
Also in 3 projects that use this
DatabricksSkillshadcn
Manages shadcn components and projects — adding, searching, fixing, debugging, styling, and composing UI. Provides project context, component docs, and usage examples. Applies when working with shadcn/ui, component registries, presets, --preset codes, or any project with a components.json file. Also triggers for "shadcn init", "create an app with --preset", or "switch to --preset".
Also in 3 projects that use this
Other versions18 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 18
- involvex/youtube-music-cli★ 490same name
- Rain-kl/OpenFlare★ 305same name
- xfstudio/skills★ 9same name
- GPTtang/skill-atlas★ 4same name
- ai4brands-design/claude-skills★ 2same name
- Aver005/agent-skills★ 2same name
- CodeWithBehnam/cc-docs★ 2same name
- Ghosteken/agent-harness★ 2same name
- gromov0737/skills★ 2same name
- roshankodi/agent-skills★ 2same name
8 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
4 rules. 0 are common, 1 is distinctive, and 3 are very specific to this project.
0
Common
Most of the 17 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 51
Use TanStack Query (v5) — or SWR if you prefer a smaller surface.
- Very specific: line 78
Do not hand-setState the response of a mutation into a list.
- Very specific: line 136
Do not mirror a form into state field-by-field unless you need live character-count validation *while typing*.
- Very specific: line 150
If you find yourself writing isLoading && !error ?
“Common” is measured: kept by at least 6 in 10 of the 17 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.
Files
React State Management
State placement is a design decision, not a preference. Most state-management migrations happen because the placement was never made deliberately, so the app ends up with the same data in a component, a context, and a store.
The default, in order
Pick the topmost rung that fits. Moving state up the list is cheap; moving it back down is a refactor.
| # | Kind of state | Default home | Example |
| 1 | Server state (anything a fetch owns) | Query library cache | Users, orders, list results, feature flags |
| 2 | Shareable UI state | URL (searchParams, hash, path) | Current tab, filters, sort, open modal id, page |
| 3 | Form/draft state | Form library or local useState | Signup form, multi-step wizard, inline edit |
| 4 | Local component UI state | useState / useReducer in the component | Is the dropdown open, is the row expanded |
| 5 | Cross-cutting client state | Small store (Zustand) | Auth session, feature entitlement, theme, onboarding-seen |
| 6 | Complex, audited, cross-app state | Redux Toolkit (only with a real reason) | Large apps, time-travel debugging, complex write paths |
The house default, stated once: server state via TanStack Query; local UI state local; URL state in the URL; global client state in a small Zustand store. Reach for Redux Toolkit only when the write paths are complex enough that you want named actions and a devtools time-line — that is a real reason, not a default.
Rung 1 — Server state is not your state
Server state has different rules from client state: it can be stale, refetched,
cached, invalidated, shared by many components, and it has loading and error
states that a useState+useEffect pair does not model.
The tell that you've reinvented it badly:
// You have written a query library, badly, in every component that loads data.
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => { /* fetch, race handling, retry, cancel */ }, [id]);Use TanStack Query (v5) — or SWR if you prefer a smaller surface. One provider at the root, then:
// Query keys are the cache identity. Give them a factory so typos are impossible.
const orderKeys = {
all: ['orders'] as const,
list: (filters: Filters) => [...orderKeys.all, 'list', filters] as const,
detail: (id: string) => [...orderKeys.all, 'detail', id] as const,
};
const { data, isPending, error, refetch, isFetching } = useQuery({
queryKey: orderKeys.list(filters),
queryFn: ({ signal }) =>
fetch(`/api/orders?${qs(filters)}`, { signal }).then(checkStatus),
staleTime: 30_000, // don't refetch on every focus for 30s
});
// Invalidate by prefix — every list variant refetches.
await queryClient.invalidateQueries({ queryKey: orderKeys.all });Rules that matter:
enabledbeats a mounted flag.useQuery({ queryKey, queryFn, enabled: !!id })— no manualuseEffectto skip the fetch.- Mutations invalidate.
onSuccess: () => queryClient.invalidateQueries(...). Do not hand-setStatethe response of a mutation into a list; you will miss the other three places that show the same data. staleTimeis per query, not global. GlobalstaleTime: Infinitymeans your app never shows fresh data and you don't notice.- Optimistic updates need
onMutate+onErrorrollback +onSettledinvalidate, all three. Skipping the rollback leaves a rejected change on screen. queryKeymust contain every value thequeryFnreads. Apageorfilterread from a closure but not in the key means the second page shows the first page's data.
Rung 2 — If the back button should work, it's URL state
Tabs, filters, pagination, sort order, search query, which entity's drawer is open, wizard step. Anything a user would expect to survive a refresh or be linkable.
// Next.js App Router
const router = useRouter();
const searchParams = useSearchParams();
const page = Number(searchParams.get('page') ?? 1);
const setPage = (n: number) => {
const next = new URLSearchParams(searchParams);
next.set('page', String(n));
router.replace(`?${next}`, { scroll: false }); // replace for filters, push for nav
};Gotchas:
useSearchParamsis a client hook. ASuspenseboundary is required during static rendering, or Next will bail out of the static shell.- Write a search input to the URL on a debounce, not on every keystroke —
otherwise every character is a history entry and a re-render storm. Use
router.replace, andpushonly for genuinely navigable steps. - The URL is a public contract. Parse and validate it; a hand-edited
?page=-1or?filter=<script>must not crash the page. - The URL is not for high-frequency state: cursor position, hover, an open native select. It serialises on every change and it is a string.
Rung 3 — Forms
Forms are a fourth category, not a useState exercise. Use a form library
(React Hook Form; Zod for the schema) when the form has more than a couple of
fields or any async validation.
const schema = z.object({
email: z.string().email(),
seats: z.coerce.number().int().min(1).max(50),
});
type Form = z.infer<typeof schema>;
const form = useForm<Form>({ resolver: zodResolver(schema), defaultValues: {...} });
const { register, handleSubmit, formState: { errors, isSubmitting } } = form;Do not mirror a form into state field-by-field unless you need live
character-count validation while typing; a watch() subscription is enough.
And do not hand-roll onChange handlers that duplicate what register does.
Rung 4 — Local state, honestly
useState for independent values, useReducer when transitions must be atomic
or the state machine has more than two or three states:
type Status = 'idle' | 'loading' | 'success' | 'error' | 'empty';
// Two useState booleans can render (loading=true, error=Error) — impossible states.If you find yourself writing isLoading && !error ? 'loading' : ..., you want
a union type, not the boolean. See references/state-placement-heuristics.md.
Rung 5 — Global client state, sparingly
Rung 5 is for state that is (a) genuinely global, and (b) genuinely not server
state and not URL state. Typically: the auth session's non-fetched parts, a
"has completed onboarding" flag, the active workspace, an entitlement
(isPro), the theme, an undo/redo buffer.
Zustand's shape — one store, selectors, no provider nesting:
// sessionStore.ts
type Session = { userId: string; entitlements: string[] } | null;
export const useSession = create<{ session: Session; setSession: (s: Session) => void }>(
(set) => ({ session: null, setSession: (session) => set({ session }) }),
);
// In a component — subscribe to the slice, not the store:
const userId = useSession((s) => s.session?.userId);Rules:
- Select the narrowest slice.
useStore()with no selector re-renders on every store change; that is the store equivalent of a context storm. - Actions live in the store, not in components, so components don't need
effect-based persistence:
export const useTheme = create<ThemeState>()( persist((set) => ({ theme: 'light', toggle: () => set((s) => ({ theme: s.theme === 'light' ? 'dark' : 'light' })), }), { name: 'theme' }), // persist via zustand/middleware, not a useEffect ); persistmiddleware, not auseEffectthat writes to storage. The effect version has a first-render read-after-write flash and a write on every render.- Redux Toolkit when you need it: complex write paths, many interacting
slices, a requirement for a serialisable, auditable action log, a team
convention. Use RTK's
createSliceandcreateAsyncThunk; do not hand-roll action types, and do not put server data in Redux — use RTK Query, which is the Redux answer to rung 1. - Zustand vs Redux is not a real technical debate. Zustand is less ceremony and less tooling; RTK is more structure and devtools. Pick one, don't have both, and don't migrate for a benchmark.
Anti-patterns to reject in review
1. Mirroring props into state. Two sources of truth, one render behind,
plus a useEffect to keep them in sync. Use the prop, or key the component:
// Reject
const [name, setName] = useState(props.name);
useEffect(() => setName(props.name), [props.name]);
// Reject harder
useEffect(() => setName(p), [p]); // p is a new object each render → loop
// Fix: read the prop. Remount with a key when the input resets.
<ProfileForm key={user.id} initial={user} />2. Context as a state manager. Context broadcasts to every consumer. A
provider whose value is a new object literal re-renders every consumer on
every provider render, and React does not bail out on context value identity the
way memo does for props.
// Storm: value is a new object every render, so all consumers re-render.
<ThemeContext.Provider value={{ theme, setTheme }}>
// Better: split contexts by update frequency, or push state to a store.
const ThemeCtx = createContext<string | null>(null); // narrow
const ThemeDispatchCtx = createContext<(t: string) => void>(() => {});
// Best for global app state: a Zustand store with a selector — no provider at all.Rules: split dispatch from state, memoise the value, put state in a store if it is global, and never put fast-changing values (cursor, drag position, keystroke buffer) in context.
3. A store for what is really a URL param or server state. A currentTab,
filters, page, or activeId in Zustand is a bug the first time someone hits
the back button or shares a link. Put it in the URL. A users array in Zustand
is a query cache that you now have to invalidate by hand.
4. Global "god store". One store for the whole app means every feature touches the same file and every change risks a circular import. Slice by concern, one file per concern, and keep the session/theme/app-shell store separate from feature state.
5. useReducer for two booleans, useState for a five-state machine. Pick
the one that matches the shape, not the one that is more impressive.
6. Optimistic state with no invalidation. Show the optimistic value, then invalidate on settle, so any component reading the same key converges.
Diagnosing re-render storms
- React DevTools Profiler → "Why did this render?" — it names the prop or hook change.
- Log a render count per suspect component; if it grows without a state change, a context value or store selector is the cause.
React.memois a last resort, not a fix. A memoised child whose props include an inline function or object literal re-renders anyway; stabilise the prop first (useCallback/ hoist it / pass an id).
Gotchas
- In React 18 StrictMode, effects run twice on mount in development only —
mount, unmount, remount. A
useEffectthat creates a WebSocket, a timer, or a subscription without cleaning up in the return leaks a duplicate in dev and looks like a production bug that only reproduces in development. Cleanup is the only fix; you cannot suppress it. - A
useEffectkeyed on an object or array literal loops forever.useEffect(() => setState(props.options), [props.options])whereprops.optionsis rebuilt inline each render: setState schedules a render, the render builds a new array, the effect fires again, forever in every environment. Depend on a primitive — a joined string or a length — or hoist the literal. React.memodoes not stop a context re-render.memobails out on equal props, but a consumer of a changed context value re-renders regardless of whether its props moved. Memoising children inside a provider that changes every render buys nothing; the provider's value identity is the whole problem.- The stale-closure bug is an omitted dependency, not a
useCallback. An effect readingfiltersfrom props without listingfilterscaptures the first render's value forever; the callback identity is irrelevant. List the dependency or hold the value in auseRefand read the ref inside. - A request's late response overwrites newer data. The
useState+useEffectversion has no abort, so page 2's slower response lands after page 3's and you render page 2 for page 3. Pass thesignalfrom the TanStack QueryqueryFnand checksignal.aborted; also key results by generation if you keep the hand-rolled version. useSyncExternalStore(and therefore every Zustand selector) compares byObject.ison the selected slice. A selector returning an object or array literal (s => ({ a: s.a, b: s.b })) returns a new reference every render, so React concludes the store changed and re-renders on every store write. Select primitives, or memoise withuseShallow.useReducer's dispatch is stable for the reducer's whole life — that is whydispatchis the one dependency you may omit. The same is true ofsetStatefromuseState, which is why so many bad examples list it. The instability is in the state and callbacks you read, not the setter.- A list keyed by the index reorders content and breaks the DOM node's
identity, so focus, selection, scroll position, and uncontrolled input values
follow the index instead of the item. Key by a stable server id; a
keyon a changing value remounts the node and throws away its state. - Zustand's
persistrehydrates asynchronously on the first render, so the store starts at its default (theme: 'light') and flips on the next tick — a hydration mismatch and a theme flash. Gate on a rehydrated flag, or useskipHydrationplus an explicit call in an effect. - A
useSearchParamswrite on every keystroke in Next.js App Router transitions the whole route and pushes a history entry per character, so Back steps through the query string character by character. Debounce and userouter.replace; reservepushfor steps a user would call a destination. - Two sources of truth for the same server entity never converge without invalidation. After a mutation, hand-set a component's state and it drifts from the query cache until the next refetch; the second render of the page shows the stale half. Let the cache own it and invalidate.
- Derived state in an effect is an extra render and a source of stale frames.
Computing a filtered list or a total with
useState+useEffectgives you one paint with the unfiltered value. Compute it during render, oruseMemoit.
Review checklist
- Does each piece of state have exactly one home?
- Is any server-cached object also in a component or store?
- Would a refresh or a shared link preserve the state the user expects?
- Do context values have stable identities and split state from dispatch?
- Do store components select the narrowest slice?
- Does every mutation invalidate the queries it affects?
- Is any prop mirrored into state "to make it editable"? (use
key)
Files
references/state-placement-heuristics.md— the "which rung?" decision procedure, with the tell-tale symptoms that a state is in the wrong place.
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
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.

