Sub topic 11
When Lifting Gets Heavy: Prop Drilling
This chapter assumes you can write function components, use useState, pass props and callbacks, and have seen a lifted-state example such as the temperature converter. Here you will see what happens when lifted state has to travel through many layers of components, why that becomes a problem, and what to try before reaching for a new tool.
What prop drilling is
Lifting state up puts the state in the closest common parent. That works well when the parent and the components that use the state are neighbors. In a real app, the consumer is sometimes five or six levels below the owner.
React props only flow from a parent to its direct children. If a deeply nested component needs a value, every component between the owner and the consumer must receive that value and pass it on. This is called prop drilling: the value is drilled through layers that do not use it.
A component that only forwards props is often called a pass-through component. It has to know about data it does not care about.
A worked example
Suppose an app keeps the current user's theme choice in App. A button deep inside the page needs to read it and change it.
import { useState } from 'react';
function ThemeButton({ theme, onToggle }) {
return (
<button onClick={onToggle}>
Current theme: {theme}. Switch.
</button>
);
}
function Toolbar({ theme, onToggle }) {
return (
<div>
<ThemeButton theme={theme} onToggle={onToggle} />
</div>
);
}
function Header({ theme, onToggle }) {
return (
<header>
<Toolbar theme={theme} onToggle={onToggle} />
</header>
);
}
function Page({ theme, onToggle }) {
return (
<main>
<Header theme={theme} onToggle={onToggle} />
</main>
);
}
export default function App() {
const [theme, setTheme] = useState('light');
return (
<Page
theme={theme}
onToggle={() => setTheme(theme === 'light' ? 'dark' : 'light')}
/>
);
}Page, Header, and Toolbar never use theme or onToggle. They exist in this chain only to carry the two props to ThemeButton. The state is correctly lifted. The path from owner to consumer is what has become costly.
Why it hurts
Prop drilling is not a bug. The code works and the data flow is easy to trace. The cost appears as the code grows.
- Noise. Intermediate components list props they never read, so it is harder to see what each component actually depends on.
- Coupling. Adding a new value for the deep component means editing every component on the path. Renaming a prop means editing them all.
- Weaker reuse.
Toolbarcannot be dropped into another screen without that screen supplyingthemeandonToggle, even ifToolbaritself does nothing with them. - Wider re-renders. When
Appstate changes,Appre-renders and, by default, so does every child in the chain. This is usually fast enough, but the chain widens the area that updates. - Harder refactoring. Moving a component to a different place in the tree can break the chain of props it depended on.
How much drilling is acceptable
Two or three levels of drilling is normal and often the clearest choice. The data path is visible in the code, and tools such as editors and type checkers can follow it. Do not treat every pass-through as a problem.
Look for these signs that drilling has become heavy:
- The same props appear in four or more consecutive components.
- Middle components accept many props only to forward them.
- Adding one new piece of shared data touches many files.
- You find yourself using
...propsspreads to avoid typing the forwarded names.
The last sign deserves care. Spreading {...props} hides which values are being forwarded, which makes the problem harder to see without removing it.
Option 1: pass fewer props by grouping them
If many related values travel together, you can pass one object instead of several separate props.
function Toolbar({ themeControl }) {
return <ThemeButton {...themeControl} />;
}
export default function App() {
const [theme, setTheme] = useState('light');
const themeControl = {
theme,
onToggle: () => setTheme(theme === 'light' ? 'dark' : 'light'),
};
return <Page themeControl={themeControl} />;
}This shortens the prop lists, but the pass-through components still exist. It also creates a new object on every render, which matters if a child is wrapped in React.memo. Treat it as a small tidy-up, not a fix.
Option 2: restructure with composition
Often the better first move is to change the component tree so fewer layers need the data. A component can accept other components as children or as named props. The owner then builds the deep component itself and hands it down already connected.
import { useState } from 'react';
function ThemeButton({ theme, onToggle }) {
return (
<button onClick={onToggle}>
Current theme: {theme}. Switch.
</button>
);
}
function Toolbar({ children }) {
return <div>{children}</div>;
}
function Header({ children }) {
return <header>{children}</header>;
}
function Page({ children }) {
return <main>{children}</main>;
}
export default function App() {
const [theme, setTheme] = useState('light');
return (
<Page>
<Header>
<Toolbar>
<ThemeButton
theme={theme}
onToggle={() => setTheme(theme === 'light' ? 'dark' : 'light')}
/>
</Toolbar>
</Header>
</Page>
);
}Now App renders ThemeButton directly and passes it the props. Page, Header, and Toolbar only provide layout and know nothing about themes. The data path is one hop long. This is sometimes called inversion of control: the owner decides what goes inside the layout components instead of the layout components deciding what to render.
Composition works best when the middle components are layout or wrapper components. It is less suitable when a middle component must make decisions based on the data, such as rendering different children depending on theme.
Option 3: use a named slot prop
When a layout component has several places for content, use named props instead of one children.
function Layout({ sidebar, content }) {
return (
<div className="layout">
<aside>{sidebar}</aside>
<section>{content}</section>
</div>
);
}
export default function App() {
const [filter, setFilter] = useState('');
return (
<Layout
sidebar={<FilterBox value={filter} onChange={setFilter} />}
content={<ResultList filter={filter} />}
/>
);
}Layout knows nothing about filter. App wires FilterBox and ResultList to the same state, which is exactly the lifting pattern, with the layout kept out of the data path.
When composition is not enough
Some situations remain where drilling is the honest cost of the design:
- The data is needed by many components at different depths, such as the current user, locale, or theme.
- The middle components cannot be restructured because they are shared or owned by a library.
- The deep component must be created by a middle component, for example inside a list that maps over items.
For these cases, React offers context, which lets a component read a value from an ancestor without receiving it through props. Context, and other options such as state libraries, are covered in the next chapter. Context has its own costs, so it is a later step, not the default response to a long prop list.
Decision guide
| Situation | Reasonable choice |
|---|---|
| Owner and consumer are 1 to 3 levels apart | Keep passing props |
| Middle components are plain layout wrappers | Restructure with `children` or slot props |
| Many props always travel together | Group them, then reconsider the structure |
| Same global-style value needed at many depths | Consider context |
| State is only used in one subtree | Move it down into that subtree |
The last row connects to an earlier idea. If drilling is heavy because the state sits too high, check whether it can move closer to its consumers before adding any new mechanism.
Common mistakes
- Lifting too far. State placed at the app root "just in case" creates long chains. Place it at the closest common parent of the components that use it.
- Treating every forwarded prop as a defect. Short chains are cheap and readable. Refactor when the cost is real.
- Hiding the chain with spreads.
{...props}shortens the code but does not shorten the path, and it can pass unrelated props to DOM elements. - Reaching for context immediately. Context makes dependencies implicit, so a component's inputs are no longer visible in its signature. Try composition first.
- Passing a new object each render to a memoized child. Grouping props into an inline object defeats
React.memobecause the reference changes every time.
Exercise
An App component owns selectedId state. It renders Page, which renders Sidebar, which renders ItemList. Only ItemList reads selectedId and calls setSelectedId. Page and Sidebar are plain layout wrappers with no logic of their own. Describe how you would remove the pass-through props without changing where the state lives, and name the term for what Page and Sidebar are doing today.
Show answer
Change Page and Sidebar to render children (or named slot props), and have App create <ItemList selectedId={selectedId} onSelect={setSelectedId} /> itself and nest it inside <Page><Sidebar>...</Sidebar></Page>. The state stays in App, but Page and Sidebar no longer receive or forward selectedId or setSelectedId. Today they are pass-through components, and the pattern of forwarding props through them is prop drilling.
Exercise
A teammate fixes a long prop chain by replacing the individual props with {...props} in every middle component. Does this remove prop drilling? Give one downside.
Show answer
No. The values still travel through every middle component, so the drilling path is unchanged. The spread only hides it. Downsides include making it unclear which props each component depends on, and passing unrelated props to child components or DOM elements, which can cause warnings or unintended behavior.