React Tutorial 0/120 lessons ~6 min read Lesson 61

    State Management Basics

    State Management Basics covers UI state ownership, local updates, derived values, shared versus server state, and choosing useState or useReducer.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Course: React — State Management Lesson: 01

    Learning objectives

    Understand what application state is, why React components need it, how state changes drive rendering, and how to choose an appropriate state-management approach.

    What is state?

    State is data that can change over time and influences what an application displays or how it behaves. Examples include a selected tab, shopping-cart items, the signed-in user, an order list received from an API, and whether a dialog is open.

    In React, state is not simply a variable. React must be informed when a value changes so it can schedule the relevant render.

    javascript
    function Counter() {
    const [count, setCount] = useState(0);
    return (
    <button onClick={() => setCount(current => current + 1)}>
    Count: {count}
    </button>
    );
    }

    Calling the setter schedules an update. Do not mutate state objects or arrays directly.

    Main categories of application state

    • Local UI state: modal visibility, selected tab, expanded panel.
    • Shared client state: cart, user preferences, active workspace.
    • Server state: customers, products, orders, invoices retrieved from a backend.
    • URL state: search query, selected page, filters, selected resource identifier.
    • Form state: field values, validation messages, dirty and submitting status.

    These categories have different ownership and lifecycle requirements. Treating all of them as one global state problem creates unnecessary complexity.

    The React state lifecycle

    A typical interaction follows this flow:

    text
    User interaction
    |
    v
    State setter / dispatch
    |
    v
    React schedules render
    |
    v
    Component reads new state
    |
    v
    UI reflects the new state

    State is a snapshot for a particular render. When the next state depends on the previous value, use a functional update:

    javascript
    setCount(previous => previous + 1);

    State should be minimal

    Avoid storing values that can be calculated from existing state.

    javascript
    const [items, setItems] = useState([]);
    const total = items.reduce((sum, item) => sum + item.price, 0);

    Storing both items and a separately maintained total can create synchronization bugs. Derive total unless there is a clear reason to persist it.

    Choosing a tool

    Start with useState for simple component state and useReducer for related state transitions. Lift state to a common parent when a few components share it. Use Context or a client-state library when many distant components need shared client state. Use a server-state tool for caching and synchronizing remote data.

    Common mistakes

    • Putting every value into a global store.
    • Mutating arrays and objects in place.
    • Duplicating derived values in state.
    • Confusing server data with temporary UI state.
    • Choosing a library before understanding the ownership problem.

    Best practices

    Keep state minimal, immutable, and close to its consumers. Give each state value a clear owner. Select tooling based on the data lifecycle and application requirements.

    Interview questions & answers

    Interview preparation

    5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.

    Beginner

    2
    1. 1What is state in React?
      Beginner

      Model answer

      State is data a component owns that can change over time and should trigger a re-render when updated through React's state APIs.

    2. 2Why should derived values usually not be stored separately?
      Beginner

      Model answer

      Derived values can be calculated from existing props or state during render.

      Storing them separately risks drift and extra update logic.

    Intermediate

    2
    1. 3Why does updating a normal variable not trigger a render?
      Intermediate

      Model answer

      Ordinary variables are not tracked by React.

      Only state and props changes schedule updates, so mutating a local variable leaves the UI unchanged.

    2. 4When would you use useReducer instead of useState?
      Intermediate

      Model answer

      Prefer useReducer when transitions are complex, related fields update together, or an explicit action/reducer model makes the flow clearer than many independent setters.

    Advanced

    1
    1. 5What is the difference between local, shared, and server state?
      Advanced

      Model answer

      Local state belongs to one component or nearby tree.

      Shared client state is owned across features.

      Server state is remote data cached and synchronized from the backend.

    Practice

    Build a small product screen with local tab state, a cart shared by header and product cards, URL-based search filters, and API-loaded product data. Identify the owner and lifecycle of every state value.

    Ready to mark this lesson complete?Track your journey across the entire course.