About 15 minutes of active study
Include time to predict, run, change, and explain the code. This is a suggested pace, not a time limit.
Context isn't the only cure for prop drilling, and often not the best one.
Here, user is passed from App through Layout and Sidebar to
UserBadge. Only UserBadge uses it:
export default function Layout ({ user }) {
return (
< div className = "board" style = { { gridTemplateColumns : "12rem 1fr" } } >
< Sidebar user = { user } />
< section className = "panel" style = { { marginTop : 0 } } >
< h2 >Today</ h2 >
< p >Three cards to study.</ p >
</ section >
</ div >
);
}
export default function Sidebar ({ user }) {
return (
< aside className = "column" >
< h2 >You</ h2 >
< UserBadge user = { user } />
</ aside >
);
}
A drilled prop React · 6 files import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
import Layout from "./Layout.jsx";
const user = { name: "Ada", streak: 4 };
export default function App() {
return (
<main>
<h1>Study Board</h1>
<Layout user={user} />
</main>
);
}
import Sidebar from "./Sidebar.jsx";
export default function Layout({ user }) {
return (
<div className="board" style={{ gridTemplateColumns: "12rem 1fr" }}>
<Sidebar user={user} />
<section className="panel" style={{ marginTop: 0 }}>
<h2>Today</h2>
<p>Three cards to study.</p>
</section>
</div>
);
}
import UserBadge from "./UserBadge.jsx";
export default function Sidebar({ user }) {
return (
<aside className="column">
<h2>You</h2>
<UserBadge user={user} />
</aside>
);
}
export default function UserBadge({ user }) {
return (
<p className="card">
<strong>{user.name}</strong>
<span className="muted"> · {user.streak}-day streak</span>
</p>
);
}
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;
}
Pass the component, not the data
Layout and Sidebar don't need user. They need to show something
in the sidebar. So let them accept the finished element:
// App.jsx
< Layout sidebar = { < Sidebar >< UserBadge user = { user } /></ Sidebar > } />
// Layout.jsx
export default function Layout ({ sidebar }) {
return (
< div className = "board" style = { { gridTemplateColumns : "12rem 1fr" } } >
{ sidebar }
< section className = "panel" style = { { marginTop : 0 } } >
< h2 >Today</ h2 >
< p >Three cards to study.</ p >
</ section >
</ div >
);
}
// Sidebar.jsx
export default function Sidebar ({ children }) {
return (
< aside className = "column" >
< h2 >You</ h2 >
{ children }
</ aside >
);
}
Make the changes and run. The page looks the same. Now user appears in only
two places: App, which owns it, and UserBadge, which shows it. Layout and
Sidebar became general-purpose frames you could reuse with anything inside.
Slots
A prop that takes JSX, like sidebar, is often called a slot . children
is the default slot; you can add named ones for each area of a layout:
header, sidebar, footer.
Choosing
Situation Use A value passed through 1–2 components Props Middle components only place things, not use them Composition: children and slots Many components at different depths need the same changing value Context
Give Layout a header slot, shown above the grid. From App, pass
<h2>Good morning, Ada</h2> into it, using user.name, so Layout still
never sees user.
Your working space React · 6 files import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
import Layout from "./Layout.jsx";
const user = { name: "Ada", streak: 4 };
export default function App() {
return (
<main>
<h1>Study Board</h1>
<Layout user={user} />
</main>
);
}
import Sidebar from "./Sidebar.jsx";
export default function Layout({ user }) {
return (
<div className="board" style={{ gridTemplateColumns: "12rem 1fr" }}>
<Sidebar user={user} />
<section className="panel" style={{ marginTop: 0 }}>
<h2>Today</h2>
<p>Three cards to study.</p>
</section>
</div>
);
}
import UserBadge from "./UserBadge.jsx";
export default function Sidebar({ user }) {
return (
<aside className="column">
<h2>You</h2>
<UserBadge user={user} />
</aside>
);
}
export default function UserBadge({ user }) {
return (
<p className="card">
<strong>{user.name}</strong>
<span className="muted"> · {user.streak}-day streak</span>
</p>
);
}
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 The caller knows what the heading should contain. The layout only needs to know where to put it.
Hint 2: a first step Layout returns a fragment, <>{header}<div ...>...</div></>, and App
builds the heading.
Show one solution // App.jsx
< Layout
header = { < h2 >Good morning, { user . name } </ h2 > }
sidebar = { < Sidebar >< UserBadge user = { user } /></ Sidebar > }
/> // Layout.jsx
export default function Layout ({ header , sidebar }) {
return (
<>
{ header }
< div className = "board" style = { { gridTemplateColumns : "12rem 1fr" } } >
{ sidebar }
< section className = "panel" style = { { marginTop : 0 } } >
< h2 >Today</ h2 >
< p >Three cards to study.</ p >
</ section >
</ div >
</>
);
} Mark as done Completion is saved in this browser only. It does not measure mastery.