Back to results for “Context API”
React › State Management

What is state management in a React app?

Beginner

Answer

State management refers to how an app organizes and shares data that changes over time, such as the current logged in user, items in a shopping cart, or a form's values. Small apps often manage this using useState and Context directly, while larger apps sometimes reach for a dedicated state management library to keep things organized.
function App() {
  const [cartItems, setCartItems] = useState([]);

  function addToCart(item) {
    setCartItems([...cartItems, item]);
  }

  return <ProductList onAddToCart={addToCart} />;
}
Real-world example A small online store manages its shopping cart entirely with useState at the top of the app, passing the cart data and update function down to whichever components need them.

Common follow-ups: At what point does an app typically need more than just useState for managing its data?;What is the difference between local state and global state?