State management
Стейт-менеджмент
State management is how an app keeps track of changing information, controls how it changes, and reflects those changes in the interface.
Also known as: управление состоянием
What counts as state?
Open a shopping site and change the sort order. The selected sort order is state: it's information the app needs to remember to display the right results. An open menu, a form draft, and the status of a network request are also examples. State management is the set of decisions about where that information lives, what can update it, and which parts of the screen depend on it. You don't need a dedicated library to manage state.
Give each piece of state an owner
A click, a keystroke, or a response from the server can trigger an update. Components that depend on the updated value can then render accordingly. If a button and a results list both depend on the selected sort order, they should read from the same source rather than keep separate copies that can drift out of sync.
- Component state: Keep a disclosure panel's open or closed flag in the component itself if nothing else needs it.
- Shared state: If two sibling components need the same value, move it to their nearest common parent. For state used across distant parts of the UI, consider a context provider or a store such as Redux. Context makes a value available to descendants; the state still needs an owner, such as a provider component using
useState. - URL state: Put filters or page numbers in the URL when users should be able to bookmark a view, share it, or retrace their steps with the Back button.
- Server state: An order history fetched from an API is a local copy of data owned by the server. A client-side cache needs rules for when to refetch that data and what to refresh after a write.
Example: derive the value you display
Suppose a React cart component receives items as a prop. It keeps the coupon checkbox in local state and calculates the total from those inputs:
const [applyCoupon, setApplyCoupon] = useState(false);
const subtotal = items.reduce((sum, item) => sum + item.priceInCents, 0);
const total = applyCoupon ? Math.round(subtotal * 0.9) : subtotal;
The checkbox handler updates applyCoupon. On the next render, the total follows from the current checkbox setting and the current items. Storing the total separately would mean updating it in two places: when the checkbox changes and when the cart contents change. For an actual checkout, the server should validate the coupon and determine the amount charged.
Common mistakes and interview trade-offs
Sharing state doesn't mean putting everything in a global store. A temporary form field often belongs in the form, even if the submitted result is shared. In React, avoid mutating a state object and passing the same reference back to its setter: React may skip the update. Create a new object with the changed fields instead.
In interviews, you may be asked to design the state for a searchable, paginated table. Explain who owns the search and page values, what should survive a reload, and how the UI handles loading, errors, and a late response for an older search. Those decisions say more about the design than the name of the library you pick.