Trivia Questions2 min readPremium

Local, Lifted, and Global State

“Where should this state live?” is one of the most common architecture questions in frontend interviews. A good answer follows one rule: keep state as close as possible to where it is used, and move it up only when you must. The React docs on sharing state between components show how local, lifted, and global state relate.

1. Local state

State owned by a single component, such as whether a dropdown is open, the current value of an uncontrolled input, or a hover flag.

function Dropdown() {
  const [open, setOpen] = useState(false);
  ...
}

Benefits: only this component re-renders when it changes, and the logic is easy to delete or move.

2. Lifted state

When two siblings need the same data, move the state to their closest common parent and pass it down via props (plus callbacks to update it).

function Filters() {
  const [query, setQuery] = useState('');
  return (
    <>
      <SearchBox value={query} onChange={setQuery} />
      <Results query={query} />
    </>
  );
}

The cost: every component between the owner and the consumer re-renders and may need to forward props (“prop drilling”).

3. Global state

State needed by many distant parts of the tree: the signed-in user, theme, feature flags, a shopping cart. Typical tools: React Context, Redux, Zustand, Jotai, or a store you write yourself.

Global state is convenient but has costs: hidden coupling, harder testing, and broad re-renders if updates are not scoped carefully.

4. Derived state: don’t store it

If a value can be computed from props or other state, compute it during render instead of syncing it with an effect.

// Avoid
const [fullName, setFullName] = useState('');
useEffect(() => setFullName(first + ' ' + last), [first, last]);

// Prefer
const fullName = first + ' ' + last;

Duplicated state drifts out of sync and causes an extra render.

5. URL state

Filters, pagination, the selected tab, and search queries often belong in the URL. The state then survives refreshes, can be shared via a link, and works with the back button.

6. Local, Lifted, and Global State: A Decision Checklist

  1. Can it be derived? Compute it.
  2. Should it survive a refresh or be shareable? Put it in the URL.
  3. Is it server data? Use a server-state cache (see the next article).
  4. Used by one component? Local state.
  5. Used by a few nearby components? Lift it to the common parent.
  6. Used across distant parts of the app? Global store or context.