State Management
Divide state into categories based on usage rather than storing everything centrally.
State Categories
| State Type | Description | Solutions |
|---|---|---|
| Component State | Local to a component, passed as props to children | useState, useReducer |
| Application State | Global UI state (modals, notifications, theme) | Context + hooks, Zustand, Jotai, Redux Toolkit, XState |
| Server Cache State | Remote data cached on the client | TanStack Query, SWR, Apollo Client, URQL, RTK Query |
| Form State | Form inputs, validation, submission | React Hook Form, Formik, React Final Form |
| URL State | Data in the address bar (params, query strings) | React Router, URL search params |
Component State
Start by defining state within the component. Elevate to a higher level only if needed elsewhere.
const [isOpen, setIsOpen] = useState(false);Use useReducer when a single action should update multiple pieces of state.
Application State
Localize state as closely as possible to the components that need it. Avoid globalizing state prematurely.
Good options:
- Zustand — minimal, hooks-based
- Jotai — atomic, great for frequent granular updates
- Redux Toolkit — structured, excellent DevTools
- XState — state machines for complex workflows
- React Context + hooks — built-in, best for low-velocity data (themes, user info)
Use use-context-selector if Context causes performance issues (most state libraries have selectors built in).
Server Cache State
Never copy query data into useState — use the query result directly.
// Don't do this
const { data } = useUsers();
const [users, setUsers] = useState(data); // stale copy
// Do this
const { data: users } = useUsers(); // always freshGood options:
- TanStack Query — REST + GraphQL
- SWR — REST + GraphQL
- Apollo Client — GraphQL
- URQL — GraphQL
- RTK Query — REST
Form State
Create abstracted Form and input field components that wrap the library and adapt to your application:
// components/ui/form/form.tsx — wraps React Hook Form
// components/ui/form/input.tsx — wraps form input primitivesIntegrate with validation libraries like Zod or Yup.
Forms can be controlled or uncontrolled — choose based on need.
URL State
Use routing solutions to access and control URL parameters:
// /app/discussions/:id
const { id } = useParams();
// /app?page=2&sort=newest
const [searchParams] = useSearchParams();URL state is ideal for filters, pagination, active tabs, and shareable views.
Key Principles
- Don't mix server and client state.
- Start local, elevate state only when necessary.
- Don't rush to Context — consider lifting state up or composition first.
- For high-frequency updates, consider atomic state libraries (Jotai) over Context.