About 20 minutes of active study
Include time to predict, run, change, and explain the code. This is a suggested pace, not a time limit.
The problem: prop drilling
The Study Board has a Density setting: compact cards hide their minutes.
The setting lives in App, but only CardItem, three levels down, uses it.
Passing it as props means Board and Column must accept a prop they never
use, just to hand it on. That's called prop drilling . It's fine for one or
two levels. For values that many distant components need, like a theme, the
signed-in user or the board's data, it gets tiring and fragile.
Context lets a parent make a value available to every component below it,
however deep, with no props in between.
A density setting through context React · 8 files import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
import { useState } from "react";
import Board from "./Board.jsx";
import { DensityContext } from "./DensityContext.js";
import { starterCards } from "./data.js";
export default function App() {
const [density, setDensity] = useState("comfortable");
return (
<main>
<h1>Study Board</h1>
<div className="toolbar">
<label>
Density{" "}
<select value={density} onChange={(e) => setDensity(e.target.value)}>
<option value="comfortable">Comfortable</option>
<option value="compact">Compact</option>
</select>
</label>
</div>
<DensityContext value={density}>
<Board cards={starterCards} />
</DensityContext>
</main>
);
}
import Column from "./Column.jsx";
import { columns } from "./data.js";
export default function Board({ cards }) {
return (
<div className="board">
{columns.map((column) => (
<Column key={column.id} title={column.title} cards={cards.filter((card) => card.column === column.id)} />
))}
</div>
);
}
import { useContext } from "react";
import { DensityContext } from "./DensityContext.js";
export default function CardItem({ card }) {
const density = useContext(DensityContext);
return (
<li className="card">
<strong>{card.title}</strong>
{density === "comfortable" && <div className="muted">{card.minutes} min</div>}
</li>
);
}
import CardItem from "./CardItem.jsx";
export default function Column({ title, cards }) {
return (
<section className="column">
<h2>
{title} <span className="count">({cards.length})</span>
</h2>
<ul className="cards">
{cards.map((card) => (
<CardItem key={card.id} card={card} />
))}
</ul>
</section>
);
}
export const columns = [
{ id: "todo", title: "To do" },
{ id: "doing", title: "Doing" },
{ id: "done", title: "Done" },
];
export const starterCards = [
{ id: 1, title: "Read about reducers", column: "todo", minutes: 20 },
{ id: 2, title: "Draw the component tree", column: "doing", minutes: 15 },
{ id: 3, title: "Practice useState", column: "done", minutes: 30 },
{ id: 4, title: "Try context", column: "todo", minutes: 25 },
];
import { createContext } from "react";
// The value used when there's no DensityContext above a component.
export const DensityContext = createContext("comfortable");
body {
font: 15px/1.5 system-ui, sans-serif;
margin: 0;
background: #f4f6f9;
color: #1c2430;
}
main {
max-width: 56rem;
margin: 1rem auto;
padding: 0 1rem;
}
h1 {
margin: 0 0 0.5rem;
font-size: 1.4rem;
}
input,
select,
button,
textarea {
font: inherit;
}
input,
select,
textarea {
padding: 0.3rem 0.5rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
}
button {
padding: 0.25rem 0.7rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
background: white;
cursor: pointer;
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
margin-bottom: 0.75rem;
}
.board {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 0.75rem;
}
.column {
background: #e9edf2;
border-radius: 10px;
padding: 0.6rem;
min-height: 6rem;
}
.column h2 {
margin: 0 0 0.5rem;
font-size: 0.95rem;
}
.count {
color: #5c6878;
font-weight: normal;
}
.cards {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.4rem;
}
.card {
background: white;
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.45rem 0.6rem;
}
.card button.title {
border: 0;
padding: 0;
background: none;
text-align: left;
font-weight: 600;
}
.card.selected {
outline: 2px solid #087ea4;
}
.muted {
color: #5c6878;
font-size: 0.85rem;
}
.panel {
margin-top: 0.75rem;
background: white;
border: 1px solid #d6dde6;
border-radius: 10px;
padding: 0.75rem 1rem;
}
.panel h2 {
margin: 0 0 0.25rem;
font-size: 1.05rem;
}
.error {
color: #b42318;
}
Switch Density to Compact and the minutes disappear. Notice that
Board.jsx and Column.jsx never mention density.
Three steps
Create the context, with a default value, in its own file:
export const DensityContext = createContext ( "comfortable" );
Provide a value around the part of the tree that needs it:
< DensityContext value = { density } >
< Board cards = { starterCards } />
</ DensityContext >
(Before React 19 this was written <DensityContext.Provider value={...}>.
You'll still see that in older code; it does the same thing.)
Use it in any component below:
const density = useContext ( DensityContext );
When density changes, every component that reads the context re-renders with
the new value, even if the components between them don't.
The nearest provider wins
useContext looks up the tree for the nearest provider. Without one, it
returns the default value from createContext. Providers can also nest: an
inner <DensityContext value="compact"> overrides the outer one for everything
inside it.
Predict
Move <Board cards={starterCards} /> outside the <DensityContext>, just
after its closing tag. Switch Density to Compact . What happens?
Check your prediction The cards keep showing their minutes. Without a provider above them, they read
the default, "comfortable", so the select no longer reaches them. No error
appears. That silence is a pitfall the third lesson deals with.
Your turn
Exercise A second setting Add a Show minutes checkbox, through a new ShowMinutesContext with the
default true. When it's unticked, cards hide their minutes whatever the
density is.
Your working space React · 8 files import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
import { useState } from "react";
import Board from "./Board.jsx";
import { DensityContext } from "./DensityContext.js";
import { starterCards } from "./data.js";
export default function App() {
const [density, setDensity] = useState("comfortable");
return (
<main>
<h1>Study Board</h1>
<div className="toolbar">
<label>
Density{" "}
<select value={density} onChange={(e) => setDensity(e.target.value)}>
<option value="comfortable">Comfortable</option>
<option value="compact">Compact</option>
</select>
</label>
</div>
<DensityContext value={density}>
<Board cards={starterCards} />
</DensityContext>
</main>
);
}
import Column from "./Column.jsx";
import { columns } from "./data.js";
export default function Board({ cards }) {
return (
<div className="board">
{columns.map((column) => (
<Column key={column.id} title={column.title} cards={cards.filter((card) => card.column === column.id)} />
))}
</div>
);
}
import { useContext } from "react";
import { DensityContext } from "./DensityContext.js";
export default function CardItem({ card }) {
const density = useContext(DensityContext);
return (
<li className="card">
<strong>{card.title}</strong>
{density === "comfortable" && <div className="muted">{card.minutes} min</div>}
</li>
);
}
import CardItem from "./CardItem.jsx";
export default function Column({ title, cards }) {
return (
<section className="column">
<h2>
{title} <span className="count">({cards.length})</span>
</h2>
<ul className="cards">
{cards.map((card) => (
<CardItem key={card.id} card={card} />
))}
</ul>
</section>
);
}
export const columns = [
{ id: "todo", title: "To do" },
{ id: "doing", title: "Doing" },
{ id: "done", title: "Done" },
];
export const starterCards = [
{ id: 1, title: "Read about reducers", column: "todo", minutes: 20 },
{ id: 2, title: "Draw the component tree", column: "doing", minutes: 15 },
{ id: 3, title: "Practice useState", column: "done", minutes: 30 },
{ id: 4, title: "Try context", column: "todo", minutes: 25 },
];
import { createContext } from "react";
// The value used when there's no DensityContext above a component.
export const DensityContext = createContext("comfortable");
body {
font: 15px/1.5 system-ui, sans-serif;
margin: 0;
background: #f4f6f9;
color: #1c2430;
}
main {
max-width: 56rem;
margin: 1rem auto;
padding: 0 1rem;
}
h1 {
margin: 0 0 0.5rem;
font-size: 1.4rem;
}
input,
select,
button,
textarea {
font: inherit;
}
input,
select,
textarea {
padding: 0.3rem 0.5rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
}
button {
padding: 0.25rem 0.7rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
background: white;
cursor: pointer;
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
margin-bottom: 0.75rem;
}
.board {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 0.75rem;
}
.column {
background: #e9edf2;
border-radius: 10px;
padding: 0.6rem;
min-height: 6rem;
}
.column h2 {
margin: 0 0 0.5rem;
font-size: 0.95rem;
}
.count {
color: #5c6878;
font-weight: normal;
}
.cards {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.4rem;
}
.card {
background: white;
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.45rem 0.6rem;
}
.card button.title {
border: 0;
padding: 0;
background: none;
text-align: left;
font-weight: 600;
}
.card.selected {
outline: 2px solid #087ea4;
}
.muted {
color: #5c6878;
font-size: 0.85rem;
}
.panel {
margin-top: 0.75rem;
background: white;
border: 1px solid #d6dde6;
border-radius: 10px;
padding: 0.75rem 1rem;
}
.panel h2 {
margin: 0 0 0.25rem;
font-size: 1.05rem;
}
.error {
color: #b42318;
}
Hint 1: the idea Create the context in its own file, provide it in App from a new piece of
state, and read it in CardItem.
Hint 2: a first step Providers nest: wrap <Board> in both. Their order doesn't matter, because
they hold different contexts.
Show one solution ShowMinutesContext.js:
import { createContext } from "react" ;
export const ShowMinutesContext = createContext ( true ); In App, a new const [showMinutes, setShowMinutes] = useState(true);, a
checkbox, and:
< DensityContext value = { density } >
< ShowMinutesContext value = { showMinutes } >
< Board cards = { starterCards } />
</ ShowMinutesContext >
</ DensityContext > In CardItem:
const density = useContext ( DensityContext );
const showMinutes = useContext ( ShowMinutesContext ); { density === "comfortable" && showMinutes && < div className = "muted" > { card . minutes } min</ div >} Mark as done Completion is saved in this browser only. It does not measure mastery.