State management
What is state management?
State is the data that describes what an application looks like and does at a given moment: whether a menu is open, what a user typed into a form, the items in a cart or the profile of the logged-in user. State management is the set of patterns and tools used to store that data, update it in a predictable way and share it between components.
In a small page, state is trivial. In a web app with dozens of screens, poor state management causes stale data, bugs that are hard to reproduce and slow interfaces.
Types of state
- Local state: belongs to a single component, such as an input value or a toggle.
- Shared state: used by several components, such as a filter that affects a list and a counter.
- Global state: needed across the whole app, such as the authenticated user or the theme.
- Server state: data fetched from an API, which can become outdated and needs caching and refetching.
- URL state: values kept in the address, such as a page number or search query, so views can be shared and bookmarked.
How it works
Most approaches follow the same loop: the interface triggers an action, the state is updated, and the interface re-renders from the new state. In a simple form, it looks like this:
const [count, setCount] = useState(0);
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>When many components need the same data, state is lifted to a common parent or moved into a dedicated store.
Common approaches compared
| Approach | Strength | Watch out for |
|---|---|---|
| Component state | Simple, built into the framework | Prop drilling when shared widely |
| Context API | No extra dependency | Re-renders on frequent updates |
| Store library (Redux, Zustand, Pinia) | Predictable, debuggable, scalable | Boilerplate and learning curve |
| Server-state library (TanStack Query) | Caching and refetching handled for you | Not meant for pure UI state |
Best practices
- Keep state as local as possible and lift it only when needed.
- Avoid duplicating the same data in several places: keep one source of truth.
- Treat server data separately from UI state.
- Derive values instead of storing them, such as a total computed from cart items.
- Make updates immutable so changes are easy to track and debug.
State management at BeBranded
The right state strategy depends on the product, not on a trend. When we build a web app, we pick the lightest approach that keeps the interface fast and the data consistent, and we document it so your team can maintain it.