State Management
Overview
Frontend state falls into distinct kinds: local UI state (a component's own data), server/async state (remote data that is cached and can go stale), and shared/global state across components. Picking the right tool for each avoids over-engineering and sync bugs. Read this when deciding where a piece of data should live or which library, if any, to introduce.
Install / setup
TanStack Query (React) — install command copied verbatim from the official installation page (tanstack.com/query/latest/docs/framework/react/installation):
npm i @tanstack/react-query
Official sources
Core concepts
- State drives the UI. You describe UI as states and update state in response to input, letting React re-render rather than mutating the DOM imperatively (react.dev/learn/managing-state).
- Keep state structure lean. Avoid redundant or duplicated state; compute derived values during render (react.dev/learn/choosing-the-state-structure).
- Lift state up to share it. Move shared state to the closest common parent and pass it down via props (react.dev/learn/sharing-state-between-components).
- Reducers for complex logic.
useReducer consolidates many related state updates into one reducer, keeping event handlers clean (react.dev/learn/extracting-state-logic-into-a-reducer).
- Context for deep passing. React context shares data deeply without prop-drilling and pairs with a reducer to scale up app state (react.dev/learn/scaling-up-with-reducer-and-context).
- Server state is different. TanStack Query treats remote data as something with a cache and lifecycle — fetched, shared, cached, refetched, and sometimes intentionally stale — distinct from client state (tanstack.com/query/latest).
Best practices
- Default to local component state; only lift it up or introduce context when multiple components genuinely need it (react.dev/learn/managing-state).
- Use a dedicated server-state library like TanStack Query for fetching, caching, and synchronizing remote data instead of hand-rolling it in component state (tanstack.com/query/latest).
- Reach for
useReducer once a component juggles several interrelated state transitions (react.dev/learn/extracting-state-logic-into-a-reducer).
- Don't store derived data in state — recompute it in render to prevent it from drifting out of sync (react.dev/learn/choosing-the-state-structure).
Common pitfalls
- Caching server data in
useState/context and manually refetching → use TanStack Query, which gives async data a cache and lifecycle (tanstack.com/query/latest).
- Duplicating one source of truth across components → lift it to a common parent or context so there is a single owner (react.dev/learn/sharing-state-between-components).
Examples
import { useQuery } from '@tanstack/react-query';
function Todos() {
const { data, isPending, error } = useQuery({
queryKey: ['todos'],
queryFn: () => fetch('/api/todos').then((r) => r.json()),
});
if (isPending) return <p>Loading…</p>;
if (error) return <p>Failed to load</p>;
return <ul>{data.map((t) => <li key={t.id}>{t.title}</li>)}</ul>;
}
Further reading
Related skills
- ../frontend-architecture — component boundaries and data flow
- ../rendering-strategies-ssr-csr — where state is hydrated and rendered
1---2name: state-management3description: Choosing between local UI state, server/async state, and global state in React apps; consult when deciding where data should live and how it updates.4domain: engineering5category: frontend6tags: [state, react, tanstack-query, server-state, context, reducers]7official_sources:8 - https://react.dev/learn/managing-state9 - https://tanstack.com/query/latest10 - https://github.com/TanStack/query11verified: 2026-06-1612metadata:13 source: https://skillmd.com/skills/gabriel7silva/state-management14---1516# State Management1718## Overview19Frontend state falls into distinct kinds: local UI state (a component's own data), server/async state (remote data that is cached and can go stale), and shared/global state across components. Picking the right tool for each avoids over-engineering and sync bugs. Read this when deciding where a piece of data should live or which library, if any, to introduce.2021## Install / setup22TanStack Query (React) — install command copied verbatim from the official installation page (tanstack.com/query/latest/docs/framework/react/installation):23```bash24npm i @tanstack/react-query25```2627## Official sources28- React state docs: https://react.dev/learn/managing-state29- TanStack Query docs: https://tanstack.com/query/latest30- Repo (TanStack Query): https://github.com/TanStack/query3132## Core concepts33- **State drives the UI.** You describe UI as states and update state in response to input, letting React re-render rather than mutating the DOM imperatively (react.dev/learn/managing-state).34- **Keep state structure lean.** Avoid redundant or duplicated state; compute derived values during render (react.dev/learn/choosing-the-state-structure).35- **Lift state up to share it.** Move shared state to the closest common parent and pass it down via props (react.dev/learn/sharing-state-between-components).36- **Reducers for complex logic.** `useReducer` consolidates many related state updates into one reducer, keeping event handlers clean (react.dev/learn/extracting-state-logic-into-a-reducer).37- **Context for deep passing.** React context shares data deeply without prop-drilling and pairs with a reducer to scale up app state (react.dev/learn/scaling-up-with-reducer-and-context).38- **Server state is different.** TanStack Query treats remote data as something with a cache and lifecycle — fetched, shared, cached, refetched, and sometimes intentionally stale — distinct from client state (tanstack.com/query/latest).3940## Best practices41- Default to local component state; only lift it up or introduce context when multiple components genuinely need it (react.dev/learn/managing-state).42- Use a dedicated server-state library like TanStack Query for fetching, caching, and synchronizing remote data instead of hand-rolling it in component state (tanstack.com/query/latest).43- Reach for `useReducer` once a component juggles several interrelated state transitions (react.dev/learn/extracting-state-logic-into-a-reducer).44- Don't store derived data in state — recompute it in render to prevent it from drifting out of sync (react.dev/learn/choosing-the-state-structure).4546## Common pitfalls47- Caching server data in `useState`/context and manually refetching → use TanStack Query, which gives async data a cache and lifecycle (tanstack.com/query/latest).48- Duplicating one source of truth across components → lift it to a common parent or context so there is a single owner (react.dev/learn/sharing-state-between-components).4950## Examples51```jsx52import { useQuery } from '@tanstack/react-query';5354function Todos() {55 const { data, isPending, error } = useQuery({56 queryKey: ['todos'],57 queryFn: () => fetch('/api/todos').then((r) => r.json()),58 });59 if (isPending) return <p>Loading…</p>;60 if (error) return <p>Failed to load</p>;61 return <ul>{data.map((t) => <li key={t.id}>{t.title}</li>)}</ul>;62}63```6465## Further reading66- https://react.dev/learn/choosing-the-state-structure — principles for shaping state67- https://tanstack.com/query/latest/docs — caching, mutations, and invalidation6869## Related skills70- ../frontend-architecture — component boundaries and data flow71- ../rendering-strategies-ssr-csr — where state is hydrated and rendered