Back to results for “Props & State”
React › Component Composition & Children Props

How does composition help avoid the problem known as prop drilling?

Intermediate

Answer

Prop drilling happens when you pass a prop through many components that do not actually need it, just so it can reach a component deep inside the tree. Composition can avoid this by passing the deeply needed component itself as children, so it sits closer to where the data actually lives, skipping the middle layers entirely.
// Instead of drilling a theme prop through three levels
function Page({ theme }) {
  return (
    <Sidebar>
      <Profile theme={theme} /> {/* passed directly as children, skipping Sidebar */}
    </Sidebar>
  );
}

function Sidebar({ children }) {
  return <aside>{children}</aside>;
}
Real-world example A large app avoids passing a user object through four layers of unrelated components by instead composing the component that actually needs the user data directly as a child, right next to where that data is available.

Common follow-ups: When is the Context API a better solution than composition for avoiding prop drilling?;What is a real world sign that a codebase has too much prop drilling?