Name Tags
Render a small board of name tags, wire a controlled greeting, and add a new tag to the list. A first React project after the early lessons.
YOUR BUILD CHECKLIST
- Map the seed people into PersonCard with a stable key on each row.
- Drive the greeting from the controlled Your name field.
- Append a new tag without the old .
- Keep the greeting test green for the current name.
STARTER
The seed data and the component files are already split apart. The search test starts red. Build the six stages in the checklist. The preview should say Search test: pass when the filter narrows the list.
import { useState } from "react"; import { people as seed } from "./data/people"; import { PersonList } from "./components/PersonList"; import { greetingPasses } from "./greeting.test"; export default function App() { const [people, setPeople] = useState(seed); const [name, setName] = useState("Ada"); const [draft, setDraft] = useState(""); const greeting = "Hello, guest"; const test = greetingPasses(name, greeting) ? "pass" : "fail"; function addPerson() { // Stage 3: append a person with a new id. Do not mutate the old array. } return ( <main> <p>Greeting test: {test}</p> <label> Your name <input aria-label="Your name" value={name} onChange={(event) => setName(event.target.value)} /> </label> <p>{greeting}</p> <label> New tag <input aria-label="New tag" value={draft} onChange={(event) => setDraft(event.target.value)} /> </label> <button type="button" onClick={addPerson}> Add tag </button> <PersonList people={people} /> </main> ); }
Solution walkthrough
Each stage points at the lesson that already taught the idea. Open a stage when you are stuck, not before you try it.
1. Render the list
Uses JSX and components, Rendering lists
PersonCard is ready. In PersonList, map people to a card and put person.id on each li. Empty ul is the starter’s unfinished work.
Open the lesson2. Controlled greeting
Uses Events and state
name already lives in useState. Build greeting from that value so the paragraph matches the input. The greeting test compares the paragraph text to Hello, plus the current name.
Open the lesson3. Add a tag
Uses Events and state, Rendering lists
On Add tag, copy the people array with a new object. Give it a unique id. Clear the draft input after a successful add. Skip blank names.
Open the lesson4. One test
Uses Testing components
greeting.test.js only checks the visible greeting string. When stage 2 is done, Greeting test should read pass for Ada and stay green as you type.
Open the lessonMake it your own
The original works. These are your next experiments.
- Optional: let each card show a favorite star stored in local state.
- Optional: clear the board back to the seed list.