Sub topic 11
When lifting gets heavy: prop drilling
This chapter assumes you can write function components, use useState, pass props, and pass callbacks such as onChange. It also assumes you already know how to lift state to a common parent.
What prop drilling is
Lifting state puts the state in a parent and sends data down through props. That works well when the parent and the components that need the data are close together. As the tree grows, the owner can end up several levels above a component that needs its value.
Every component between them must accept the prop and pass it on, even when it never uses it. This is called prop drilling.
A drilled example
Here App owns a theme value. Only Button uses it, but Layout and Sidebar must carry it.
import { useState } from 'react';
function Button({ theme, onToggle }) {
return (
<button className={theme} onClick={onToggle}>
Theme: {theme}
</button>
);
}
function Sidebar({ theme, onToggle }) {
return <aside><Button theme={theme} onToggle={onToggle} /></aside>;
}
function Layout({ theme, onToggle }) {
return <main><Sidebar theme={theme} onToggle={onToggle} /></main>;
}
export default function App() {
const [theme, setTheme] = useState('light');
return (
<Layout
theme={theme}
onToggle={() => setTheme(theme === 'light' ? 'dark' : 'light')}
/>
);
}The code is correct, and for a tree this small it is fine. Drilling is not a bug.
Why it becomes a problem
Drilling has costs that grow with the tree:
- Noisy components.
LayoutandSidebarlist props they do not use, which hides what they actually do. - Fragile changes. Renaming a prop, or adding a second one, means editing every component on the path.
- Weak reuse. An intermediate component with required pass-through props is harder to use somewhere else.
- Wider re-renders. When the owner's state changes, every component on the path re-renders, because each one receives new props.
Decide whether it is a problem yet
Drilling through one or two layers is usually acceptable. Before reaching for a new tool, check these points:
- Is the state really shared, or could it live lower in the tree? See the previous chapter on pushing state down.
- How many layers does the prop cross?
- How many different props are being forwarded?
- Do the intermediate components exist only to carry data?
If the answers point to a few layers and a single prop, keep drilling. It is explicit, and you can trace where every value comes from by reading the code.
Common mistakes
- Lifting too high "just in case." State placed at the root forces drilling that a closer owner would avoid.
- Forwarding everything with
{...props}. This shortens the code but hides which values flow where, so debugging gets harder. - Treating drilling as an automatic reason for context. Context adds indirection, and any component reading it re-renders when its value changes.
What comes next
When drilling does become heavy, there are other tools: restructuring with composition, sharing values with context, or using a state library. The next chapter covers each of them and when to choose it.
Exercise
In the example, the Sidebar component never reads theme. Why must it still declare it as a prop, and what is the name for this pattern?
Show answer
Sidebar renders Button, and Button needs theme, which only reaches Sidebar through props from Layout. So Sidebar must accept theme and pass it on. This pattern is called prop drilling.
Exercise
Name two reasons to keep drilling instead of switching to context.
Show answer
Any two of: the value crosses only a few layers; only one or a few props are forwarded; drilling is explicit, so data flow is easy to trace; context adds indirection and re-renders every component that reads it.