This search box has two bugs. Type react and look at the results and the
count. Before reading on, write down what's wrong.
npm install and npm run dev. Check what you found
- Typing
reactfinds nothing, although two titles contain React. - The count always says 4 matching, whatever you type.
A method for finding bugs
Guessing and changing code at random wastes time. Work through it instead:
-
Reproduce it with the smallest input. Does
Rmatch? Doesr? DoesReact? -
Check what the component actually sees. Add a
console.logjust before thereturn, during render:JavaScriptconsole.log({ query, matches: matches.map((task) => task.title) });Each keystroke re-renders, so each keystroke logs a line.
-
Compare with what you expected, step by step, from the input to the screen. The first step that's wrong is where the bug is.
Try it. With the log in place, type R, then r.
Hint 1: the first bug
R matches two tasks and r matches different ones. includes compares
exactly, so "React".includes("r") is false. Compare lower-case versions of
both sides.
Hint 2: the second bug
The log shows matches changing, but the count doesn't. Look at which array
the count uses.
Show the fixes
const needle = query.trim().toLowerCase();
const matches = tasks.filter((task) => task.title.toLowerCase().includes(needle));<p className="muted">{matches.length} matching</p>The first bug was in the data step: the comparison. The second was in the
display step: the count read tasks, the full list, instead of matches. Each
step looked plausible on its own; the log showed which one disagreed with
expectations.
Remove the console.log when you're done.
React Developer Tools
On your own computer, install the React Developer Tools browser extension. It adds a Components tab to the browser's developer tools, where you can select any component and see its current props and state, and even edit them. It can't reach inside this course's labs, which run in a sandbox, but it's the first tool to open when a downloaded project misbehaves.
Completion is saved in this browser only. It does not measure mastery.