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.
FilterableList handles the parts every list needs: the filter box, the count,
and what to show when nothing matches. How each item looks is different every
time it's used, so the caller decides.
A list the caller draws React · 5 files import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
import FilterableList from "./FilterableList.jsx";
import { topics } from "./topics.js";
export default function App() {
return (
<main>
<h1>Topics</h1>
<FilterableList
items={topics}
label="Filter topics"
getText={(topic) => topic.title}
renderItem={(topic) => (
<>
<strong>{topic.title}</strong>
<span className="muted"> · {topic.minutes} min{topic.done ? " · done" : ""}</span>
</>
)}
empty={<p className="muted">No topics match. Try a shorter word.</p>}
/>
</main>
);
}
import { useState } from "react";
// Owns filtering and the empty state. The caller decides how each item looks
// (renderItem) and what an empty list says (empty).
export default function FilterableList({ items, label, getText, renderItem, empty }) {
const [query, setQuery] = useState("");
const needle = query.trim().toLowerCase();
const shown = items.filter((item) => getText(item).toLowerCase().includes(needle));
return (
<section>
<div className="toolbar">
<input aria-label={label} placeholder={label} value={query} onChange={(e) => setQuery(e.target.value)} />
<span className="muted">{shown.length} of {items.length}</span>
</div>
{shown.length === 0 ? (
empty
) : (
<ul className="cards">
{shown.map((item) => (
<li key={item.id} className="card">{renderItem(item, { query: needle })}</li>
))}
</ul>
)}
</section>
);
}
export const topics = [
{ id: 1, title: "Compound components", minutes: 25, done: true },
{ id: 2, title: "Controlled props", minutes: 20, done: false },
{ id: 3, title: "Render props", minutes: 15, done: false },
{ id: 4, title: "Imperative handles", minutes: 20, done: false },
];
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;
}
[role="tablist"] {
display: flex;
gap: 0.25rem;
border-bottom: 1px solid #d6dde6;
margin-bottom: 0.75rem;
}
[role="tab"] {
border: 0;
border-bottom: 2px solid transparent;
border-radius: 0;
background: none;
padding: 0.4rem 0.8rem;
}
[role="tab"][aria-selected="true"] {
border-bottom-color: #087ea4;
color: #06617f;
font-weight: 600;
}
.stars button {
border: 0;
background: none;
padding: 0 0.1rem;
font-size: 1.3rem;
color: #b9c2cd;
}
.stars button[aria-checked="true"],
.stars button.on {
color: #d99a00;
}
Filter for props , then for something that matches nothing.
A render prop
renderItem = {(topic) => (
<>
<strong>{ topic . title }</strong>
<span className = "muted" > · {topic. minutes } min </ span >
</>
)}
renderItem is a function that returns JSX . FilterableList calls it for
each visible item, and puts the result inside its own <li>. The list owns the
structure; the caller owns the content. This pattern is called a render
prop :
< ul className = "cards" >
{ shown . map (( item ) => (
< li key = { item . id } className = "card" > { renderItem ( item , { query : needle }) } </ li >
)) }
</ ul >
The second argument passes extra information the caller might want, here the
current search text: renderItem(item, { query }). Libraries often pass state
this way, such as whether an item is highlighted.
A slot
empty is a plain element, not a function: a slot like the ones in the
intermediate course. Use a slot when the content doesn't depend on the
component's internal state, and a render prop when it does.
Custom hooks instead?
Render props were common before hooks, for sharing logic as well as looks.
Today, shared logic belongs in a custom hook. Render props remain the right
tool when the component needs to control where something renders, as
FilterableList does with each <li>.
Exercise Highlight the match Use the query that renderItem receives to wrap the matching part of each
title in <mark>. Filtering for prop shows Controlled prop s .
Your working space React · 5 files import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
import FilterableList from "./FilterableList.jsx";
import { topics } from "./topics.js";
export default function App() {
return (
<main>
<h1>Topics</h1>
<FilterableList
items={topics}
label="Filter topics"
getText={(topic) => topic.title}
renderItem={(topic) => (
<>
<strong>{topic.title}</strong>
<span className="muted"> · {topic.minutes} min{topic.done ? " · done" : ""}</span>
</>
)}
empty={<p className="muted">No topics match. Try a shorter word.</p>}
/>
</main>
);
}
import { useState } from "react";
// Owns filtering and the empty state. The caller decides how each item looks
// (renderItem) and what an empty list says (empty).
export default function FilterableList({ items, label, getText, renderItem, empty }) {
const [query, setQuery] = useState("");
const needle = query.trim().toLowerCase();
const shown = items.filter((item) => getText(item).toLowerCase().includes(needle));
return (
<section>
<div className="toolbar">
<input aria-label={label} placeholder={label} value={query} onChange={(e) => setQuery(e.target.value)} />
<span className="muted">{shown.length} of {items.length}</span>
</div>
{shown.length === 0 ? (
empty
) : (
<ul className="cards">
{shown.map((item) => (
<li key={item.id} className="card">{renderItem(item, { query: needle })}</li>
))}
</ul>
)}
</section>
);
}
export const topics = [
{ id: 1, title: "Compound components", minutes: 25, done: true },
{ id: 2, title: "Controlled props", minutes: 20, done: false },
{ id: 3, title: "Render props", minutes: 15, done: false },
{ id: 4, title: "Imperative handles", minutes: 20, done: false },
];
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;
}
[role="tablist"] {
display: flex;
gap: 0.25rem;
border-bottom: 1px solid #d6dde6;
margin-bottom: 0.75rem;
}
[role="tab"] {
border: 0;
border-bottom: 2px solid transparent;
border-radius: 0;
background: none;
padding: 0.4rem 0.8rem;
}
[role="tab"][aria-selected="true"] {
border-bottom-color: #087ea4;
color: #06617f;
font-weight: 600;
}
.stars button {
border: 0;
background: none;
padding: 0 0.1rem;
font-size: 1.3rem;
color: #b9c2cd;
}
.stars button[aria-checked="true"],
.stars button.on {
color: #d99a00;
}
Hint 1: the idea Split the title into three pieces: the text before the match, the match, and
the text after. To find where the match starts regardless of case, search a
lower-case copy of the title. With an empty query, or no match, there's only
one piece: the plain title.
Hint 2: a first step Write a small Highlight component that takes text and query, and work out
where the match starts. Check it with a console.log: for Controlled props
and prop , it's 11. Then return the three pieces, with the middle one inside
<mark>. Which string method cuts out the text between two positions?
Show one solution function Highlight ({ text , query }) {
const at = query ? text . toLowerCase (). indexOf ( query ) : - 1 ;
if ( at === - 1 ) return text ;
return (
<>
{ text . slice ( 0 , at ) }
< mark > { text . slice ( at , at + query . length ) } </ mark >
{ text . slice ( at + query . length ) }
</>
);
} renderItem = {( topic , { query }) => (
<>
<strong><Highlight text = {topic. title } query = { query } /></ strong >
<span className = "muted" > · {topic. minutes } min {topic.done ? " · done" : "" } </ span >
</>
)} Mark as done Completion is saved in this browser only. It does not measure mastery.