Course contentsShow
← React
Lesson 2256 of 2,39649. Performance: Identifying ProblemsFree lesson

When Context is Wrong: Lift Content Up Instead

Recognizing scenarios where passing JSX as children avoids context entirely, keeping slow-updating content outside the re-render tree.

When Context is Wrong: Lift Content Up Instead

You'll learn: How to avoid context entirely by passing slow-updating content as children, keeping it outside the re-render tree.

The Core Idea

Sometimes the "performance problem" with context isn't the context itself—it's that you're putting the wrong things inside it. When a parent component owns fast-changing state but also renders slow, expensive children, those children re-render unnecessarily. The solution? Don't use context at all. Instead, pass the slow content as children from a higher level, so it stays mounted outside the updating component's tree.

The Pattern

Imagine a Dashboard component that tracks mouse position (fast updates) and also renders a heavy ChartPanel. Every mouse move re-renders ChartPanel even though it doesn't need the mouse data.

Before (bad):

function Dashboard() {
  const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
  // ChartPanel re-renders on every mouse move
  return <ChartPanel />;
}

After (lifted content):

function Dashboard({ children }) {
  const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
  // children (ChartPanel) was created by parent, stays stable
  return <div>{children}</div>;
}

// In parent:
<Dashboard>
  <ChartPanel />
</Dashboard>

Now ChartPanel is created outside Dashboard. When Dashboard re-renders, children is the same reference—no re-render of ChartPanel.

When This Applies

Use this when:

  • A component owns fast-updating state
  • But also renders slow children that don't need that state
  • The children can be passed from above as children or props

Key Takeaway: Before reaching for context to "avoid prop drilling," check if lifting slow content up as children keeps it out of the re-render path entirely—no memoization, no context, just composition.