Skip to article content
FrontendXLesson 12 / 40

State Colocation and Lifting

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

intermediate · about 50 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Interview

What you will be able to do

#
  • Decide where a piece of state should live.
  • Lift state to the closest common ancestor of its consumers.
  • Push state down to shrink the re-render scope.

Overview

#

State lives where it is used

The first question for any piece of state is who needs to read or write it. If only one component needs it, keep it in that component. Colocating state limits the components that re-render and keeps the data flow easy to follow.

snippet.tsx
function SearchBox() {
  const [query, setQuery] = useState('');
  return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
}

Lift only when shared

When two components must share the same value, move it up to their closest common ancestor and pass it down. Lifting is not free: every component between the owner and the consumer re-renders when the value changes, and each one must forward props it does not use.

snippet.tsx
function Parent() {
  const [selected, setSelected] = useState<string | null>(null);
  return (
    <>
      <List selected={selected} onSelect={setSelected} />
      <Detail id={selected} />
    </>
  );
}

Push state down

If a parent holds state only to pass it to one child, move the state into that child. A counter or a search box that was lifted for no reason is a common example. Pushing state down shrinks the re-render scope and removes props.

Pitfall: lifting too far

Putting state at the app root because it might be needed later re-renders the whole tree on every change. Lift to the closest common ancestor of the actual consumers, not to the top. If the value is truly global and changes often, use context or an external store instead of prop drilling.

A decision checklist

One consumer: keep it local. Several consumers under one parent: lift to that parent. Many distant consumers: context or a store. Value derivable from other state: do not store it at all.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    How many components re-render on each keystroke?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    function App() {
      const [query, setQuery] = useState('');
      return (
        <div>
          <Header />
          <SearchBox query={query} onChange={setQuery} />
          <Footer />
        </div>
      );
    }
    Choose the best answer
  2. Exercise 2Implement

    Refactor so the text input owns its own state and only the submitted value is lifted.

    starter.tsx
    function Search({ onSearch }: { onSearch: (q: string) => void }) {
      return null;
    }

    Reveal the reference answer
    reference.tsx
    function Search({ onSearch }: { onSearch: (q: string) => void }) {
      const [query, setQuery] = useState('');
      return (
        <form onSubmit={(e) => { e.preventDefault(); onSearch(query); }}>
          <input value={query} onChange={(e) => setQuery(e.target.value)} />
        </form>
      );
    }

    Authored expected output: Typing re-renders only Search; the parent is notified only on submit.

    The draft text is local because only this component needs it. The committed value is lifted through onSearch, so the parent updates only when the user submits.

  3. Exercise 3Interview

    A teammate lifted a filter value to the app root and now the page is slow. Walk through your fix.

    starter.tsx
    // App holds filter state and passes it through five levels.

    Reveal the reference answer
    reference.tsx
    First confirm with the profiler that the root update re-renders unrelated subtrees. Then move the filter state down to the component that owns the list, or to the closest common ancestor of the list and its controls. If distant components need it, use context or an external store with selective subscriptions instead of drilling through five levels. Re-profile to confirm the re-render scope shrank.

    Authored expected output: A measured plan: profile, relocate state to the closest consumers, use context or a store if truly shared, re-verify.

    The fix follows from the re-render scope. State placement is the lever; context and stores are alternatives when the consumers are far apart.

  4. Exercise 4Explain

    Explain the trade-off between colocating state and lifting it, using re-render scope and prop forwarding.

    starter.tsx
    // local state vs lifted state

    Reveal the reference answer
    reference.tsx
    Colocated state re-renders only the component that owns it and needs no props, but it cannot be shared. Lifted state can be shared by siblings, but it re-renders every component between the owner and the consumers and forces intermediate components to forward props. Choose the smallest scope that still lets every consumer read and write the value.

    Authored expected output: A written trade-off between shared access and re-render scope plus prop forwarding.

    There is no free option: sharing costs re-render scope. The right answer names both sides and picks the minimal sharing that satisfies the requirement.

Common misconceptions

#
  • All shared state should live at the app root. Lift to the closest common ancestor of the consumers.
  • Lifting state is free. Lifting widens the re-render scope and adds prop forwarding.
  • Prop drilling is always wrong. Drilling one or two levels is fine; refactor when it becomes wide or noisy.
  • State should be colocated even when two siblings need it. Shared state must move above both consumers.

Follow-up practice

#
  • react-derived-state
  • react-component-architecture

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.