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

    Local vs Global State

    Local vs Global State explains when state should stay local, lifting state up, prop drilling tradeoffs, and when Context or a store is justified.

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

    Course: React — State Management Lesson: 02

    Learning objectives

    Distinguish component-local state from shared application state, decide when to lift state, and avoid unnecessary global coupling.

    Local state

    Local state belongs to a component or a small subtree. Examples include:

    • Whether a dropdown is open.
    • The current value of an input.
    • Which accordion panel is expanded.
    • A temporary confirmation dialog.
    javascript
    function SearchPanel() {
    const [query, setQuery] = useState("");
    const [expanded, setExpanded] = useState(false);
    return (
    <section>
    <input value={query} onChange={event => setQuery(event.target.value)} />
    <button onClick={() => setExpanded(value => !value)}>
    Filters
    </button>
    {expanded && <FilterOptions />}
    </section>
    );
    }

    There is no benefit in putting these values into a global store if no other feature needs them.

    Lift state when siblings need the same value

    If two sibling components need to read or update the same state, move ownership to their nearest common parent and pass values and callbacks down.

    text
    ProductPage
    / \
    ProductInfo CartSummary
    \ /
    shared cart

    This is often the simplest form of shared state.

    Global state

    Global or application-level state is appropriate when unrelated parts of the application need a common client-owned value. Examples can include the current workspace, user preferences, a shared cart, or application-wide notification state.

    A global store introduces a dependency between consumers and the store contract. That dependency should be justified by actual sharing requirements.

    Decision guide

    QuestionLikely approach
    Used by one component?Local state
    Used by a small component subtree?Lift state
    Needed across distant features?Context or client-state store
    Comes from a backend and needs caching?Server-state library
    Should survive refresh or be shareable?URL or persistent storage, as appropriate

    State ownership example

    In an order-management SaaS:

    • Modal visibility belongs to the order page.
    • A filter shared by a toolbar and table can belong to the page or feature hook.
    • The selected workspace may be application-wide.
    • Order records are server state.
    • The current page and shareable filters may belong in query parameters.

    Common mistakes

    • Making every state global “for convenience”.
    • Duplicating the same state in a component and a global store.
    • Lifting state too high, causing unrelated components to re-render.
    • Using global state as a substitute for server-data caching.

    Best practices

    Use the narrowest scope that satisfies all consumers. Promote state only when a real sharing or lifecycle requirement appears. Keep server state separate from client-owned state.

    Interview questions & answers

    Interview preparation

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

    Beginner

    2
    1. 1When should state remain local?
      Beginner

      Model answer

      Keep state local when only one component or a tight parent/child group needs it—modals, draft inputs, and ephemeral UI flags are common cases.

    2. 2How do you decide between Context and a store?
      Beginner

      Model answer

      Use Context for focused dependency distribution.

      Use an external store when many consumers need selective subscriptions or richer update tooling.

    Intermediate

    2
    1. 3What does lifting state up mean?
      Intermediate

      Model answer

      Moving state to the nearest common ancestor so siblings can share the same source of truth through props.

    2. 4Why is server state not automatically global client state?
      Intermediate

      Model answer

      Server state is owned by the backend and needs caching, freshness, and invalidation.

      Dumping it into a global client store often recreates those problems poorly.

    Advanced

    1
    1. 5What are the tradeoffs of global state?
      Advanced

      Model answer

      Global state reduces prop drilling but increases coupling, makes ownership harder to see, and can cause broader re-renders if not carefully subscribed.

    Practice

    Design state ownership for a dashboard containing a sidebar, filters, user menu, order table, notification center, and detail drawer. Explain why each value belongs at its chosen level.

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