Sub topic 1
The problem: passing props through many layers
Prerequisites
This chapter assumes you know the following:
- JavaScript ES6 syntax: destructuring, arrow functions, and modules.
- React function components, JSX, and props.
- The
useStatehook. - The basics of how React re-renders a component when its state or props change.
What prop drilling is
React passes data from parent to child through props. This works well when the child that needs the data sits right below the parent. It gets awkward when the data is needed deep in the tree.
Suppose App owns the current theme, and a button three levels down needs it. The components in between do not use the theme. They only receive it and hand it on. This pattern is called prop drilling: passing a prop through components that do not use it, only so a deeper component can read it.
A worked example
The code below drills a theme prop through Toolbar to reach ThemedButton.
import { useState } from 'react';
function App() {
const [theme, setTheme] = useState('light');
return (
<div>
<Page theme={theme} />
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Toggle theme
</button>
</div>
);
}
function Page({ theme }) {
return <Toolbar theme={theme} />;
}
function Toolbar({ theme }) {
return <ThemedButton theme={theme} />;
}
function ThemedButton({ theme }) {
return <button className={`btn-${theme}`}>Save</button>;
}Only ThemedButton uses theme. Page and Toolbar accept it and forward it unchanged.
Why it hurts
Prop drilling is not a bug. The code runs correctly. The cost shows up as the app grows:
- Noisy components.
PageandToolbarlist a prop they never use. A reader has to trace the prop to see why it is there. - Harder changes. Renaming
themeor changing its shape means editing every component on the path. - Tighter coupling. Reusing
Toolbarsomewhere else forces the new parent to supply athemeprop, even thoughToolbardoes not care about it. - Easy mistakes. If one layer forgets to forward the prop, the deep component gets
undefined, and the error appears far from the cause.
The problem gets worse when many values are drilled together, such as the signed-in user, the language, and the theme. Each one adds another prop to every layer.
Where context fits in
React provides a feature called context for this situation. Context lets a component higher in the tree make a value available to every component beneath it. Any descendant can read the value directly, and the components in between do not need to know about it.
The later chapters build this up step by step. You will create a context with createContext, supply a value with a Provider, and read it with useContext. With those pieces, the example above shrinks. Page and Toolbar take no props, and ThemedButton reads the theme itself.
Recognizing the problem
Look for these signs in your own code:
- A prop appears in a component's parameters but only appears again as an attribute on a child element.
- The same prop is passed through three or more layers.
- Adding a new shared value means editing many files.
If you see these, you have a candidate for context.
Exercise
In the example, Page and Toolbar both receive a theme prop. Why is this considered prop drilling, and what is one concrete problem it causes?
Show answer
Neither Page nor Toolbar uses theme. They only forward it to ThemedButton, which is the one component that needs it. This causes problems such as noisy components, extra edits when the prop changes, and reduced reusability, because Toolbar cannot be used elsewhere without a parent supplying theme.
Exercise
You add a user value that a deeply nested Avatar component needs. List which components must change under prop drilling if App owns user and the path is App, Page, Toolbar, Avatar.
Show answer
App must pass user to Page, Page must accept it and pass it to Toolbar, Toolbar must accept it and pass it to Avatar, and Avatar must read it. That is four components edited, even though only Avatar uses the value.