Skip to content

Finding Bugs

The Course
•
15 min study

This search box has two bugs. Type react and look at the results and the count. Before reading on, write down what's wrong.

A search with two bugs React · 3 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"; const tasks = [ { id: 1, title: "Learn React components" }, { id: 2, title: "Practice props" }, { id: 3, title: "Review React state" }, { id: 4, title: "Write a form" }, ]; export default function App() { const [query, setQuery] = useState(""); const matches = tasks.filter((task) => task.title.includes(query)); return ( <main> <h1>Search tasks</h1> <label> Search{" "} <input type="text" value={query} onChange={(e) => setQuery(e.target.value)} /> </label> <p className="muted">{tasks.length} matching</p> <ul> {matches.map((task) => ( <li key={task.id}>{task.title}</li> ))} </ul> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; } label { display: block; margin: 0.5rem 0; } input, select, button { font: inherit; } input[type="text"], input[type="number"], select { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } .error { color: #b42318; margin: 0.25rem 0; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Check what you found
  1. Typing react finds nothing, although two titles contain React.
  2. 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:

  1. Reproduce it with the smallest input. Does R match? Does r? Does React?

  2. Check what the component actually sees. Add a console.log just before the return, during render:

    JavaScript
    console.log({ query, matches: matches.map((task) => task.title) });

    Each keystroke re-renders, so each keystroke logs a line.

  3. 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
JavaScript
const needle = query.trim().toLowerCase();
const matches = tasks.filter((task) => task.title.toLowerCase().includes(needle));
JSX
<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.

Was this article helpful?

Your response is saved on this device.