# State Management Principles

> Classify state (local, shared, global, URL, server, form) and put each kind in the right place before reaching for a library. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/frontend/state-management
Last updated: 2026-11-28

Classify state (local, shared, global, URL, server, form) and put each kind in the right place before reaching for a library.

Intermediate · about 3 hours · difficulty 3/5

Learn first: [Context and Component Composition](https://www.internhack.in/roadmaps/tracks/frontend/react-context), [Client-Side Routing](https://www.internhack.in/roadmaps/tracks/frontend/client-routing)

"Which state library should I use?" is usually the wrong first question. Most state problems come from putting data in the wrong place. Start by classifying it.

### Kinds of state

- **Local UI state**: is this dropdown open? Keep it in the component with `useState`.
- **Shared state**: a few nearby components need it. Lift it to their closest common parent.
- **Global client state**: theme, auth session, a shopping cart, a sidebar toggle used across the app. Context or a small store.
- **URL state**: filters, search terms, the selected tab, pagination. Use the router.
- **Server state**: data that lives in a database and is cached in the browser (products, users, messages). It can go stale and needs refetching; use a server-state library such as TanStack Query.
- **Form state**: in-progress input values, validation errors and touched fields. Use a form library for anything non-trivial.

### Principles

- Keep state **as local as possible** and lift only when needed.
- Keep a **single source of truth**; derive everything else during render.
- Do not copy server data into a global store "just in case". That creates two sources of truth and stale bugs.
- Prefer **immutable updates** so changes are predictable and debuggable.

### When a store library helps

When many distant components read and update the same client state, when updates are frequent (context would re-render too much), or when you want devtools, persistence and middleware.

### Good enough to move on

You can look at an app's features and state which kind of state each piece is and where it should live, and justify why server data should not sit in a hand-rolled global store.

## Key concepts

- **Local vs shared vs global state**
- **URL as state**
- **Server state vs client state**
- **Form state**
- **Single source of truth and derived state**
- **Immutable updates**

## Mini project

Audit a React app you built earlier: list every piece of state, label its kind, then refactor at least three pieces into a better home (URL, lifted state or a derived value) and note the bugs this removes.

## Check yourself

Why is storing a fetched product list in a global store and also in component state a recipe for stale data?

## Free resources

- [Choosing the State Structure](https://react.dev/learn/choosing-the-state-structure) (react.dev)
- [Sharing State Between Components](https://react.dev/learn/sharing-state-between-components) (react.dev)

Previous: [Client-Side Routing](https://www.internhack.in/roadmaps/tracks/frontend/client-routing) · Next: [Client State Libraries: Zustand, Redux Toolkit and Jotai](https://www.internhack.in/roadmaps/tracks/frontend/state-libraries)

## More in Routing, State and Data

- [Client-Side Routing](https://www.internhack.in/roadmaps/tracks/frontend/client-routing)
- [State Management Principles](https://www.internhack.in/roadmaps/tracks/frontend/state-management)
- [Client State Libraries: Zustand, Redux Toolkit and Jotai](https://www.internhack.in/roadmaps/tracks/frontend/state-libraries)
- [Server State with TanStack Query](https://www.internhack.in/roadmaps/tracks/frontend/server-state)
- [Working with REST APIs in Production](https://www.internhack.in/roadmaps/tracks/frontend/api-integration)
- [GraphQL on the Frontend](https://www.internhack.in/roadmaps/tracks/frontend/graphql-clients)
- [Forms in React with React Hook Form](https://www.internhack.in/roadmaps/tracks/frontend/react-forms)
- [Schema Validation with Zod](https://www.internhack.in/roadmaps/tracks/frontend/schema-validation)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Frontend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/frontend)
