Course contentsShow
← React
Lesson 2313 of 2,39650. Performance: Optimization TechniquesFree lesson

Moving State to the Component That Owns It

Refactoring to push state down to the lowest common ancestor of components that need it.

Moving State to the Component That Owns It

What you'll learn: How to refactor state placement so it lives in the lowest common ancestor component that actually needs it, reducing unnecessary re-renders across your tree.


The Core Problem

When state lives too high in your component tree, every update triggers re-renders in all descendants—even components that don't care about that state. You've already learned to identify over-hoisted state in the previous lesson. Now it's time to fix it by moving state down to the component that truly owns it.

The Refactoring Strategy

The goal is simple: push state down to the lowest common ancestor of the components that actually need to read or update it.

Before:

// State lives in App, but only SettingsPanel uses it
function App() {
  const [theme, setTheme] = useState('light');
  return (
    <>
      <Header /> {/* doesn't use theme */}
      <MainContent /> {/* doesn't use theme */}
      <SettingsPanel theme={theme} setTheme={setTheme} />
    </>
  );
}

After:

// State moved down—only SettingsPanel re-renders on change
function App() {
  return (
    <>
      <Header />
      <MainContent />
      <SettingsPanel /> {/* manages its own theme state */}
    </>
  );
}

function SettingsPanel() {
  const [theme, setTheme] = useState('light');
  // ... use theme here
}

Now when theme updates, Header and MainContent don't re-render at all—because App itself doesn't re-render.

When to Apply This

Use Profiler to confirm: if changing state causes cascading re-renders in unrelated branches, that state is a candidate for moving down. If multiple siblings need the state, move it only to their shared parent—not higher.

Key Takeaway: Refactor state to live as close as possible to where it's actually used; this isolates re-renders to the smallest subtree necessary and eliminates wasted renders in unrelated components.