Part 09 / Topic 1 of 6
What prop drilling actually costs
Passing props down one or two levels is normal React and not a problem on its own - Part 2 already does it constantly. The pain shows up when a value is needed several levels down, and every component in between has to accept and forward it even though it never reads it. examples/01_prop_drilling_pain.jsx threads a theme value and a toggleTheme function three levels deep:
// examples/01_prop_drilling_pain.jsx
function LayoutDrilling({ theme, onToggleTheme }) {
// Layout never reads theme or onToggleTheme - it exists purely to pass
// them further down to whichever grandchild actually needs them.
return (
<div>
<SidebarDrilling theme={theme} onToggleTheme={onToggleTheme} />
<MainContentDrilling />
</div>
);
}
function SidebarDrilling({ theme, onToggleTheme }) {
return <ThemeBadgeDrilling theme={theme} onToggleTheme={onToggleTheme} />;
}
This really works - clicking the toggle button really flips "Theme: light" to "Theme: dark" and back (examples/01_prop_drilling_pain.test.jsx). The cost isn't correctness, it's maintenance: LayoutDrilling and SidebarDrilling both have a theme/onToggleTheme pair in their signature that belongs, really, to ThemeBadgeDrilling three levels down - and adding a second value that needs the same trip (say, a logged-in user) means editing both pass-through components' signatures again, not just the two components that actually care.
Done here? The practice set for Part 9 — including the solutions — is on the part page.