Add items to the shopping list

React · React · beginner · modification

Households asked for a way to add things to the shared shopping list without leaving the page. `src/shopping/ShoppingList.jsx` gets a small form under the list (an "Item" field and an Add button). `addItem` reads the field through `FormData`, trims it, skips blank names, appends an unticked item with a fresh id to the list state, and resets the form. `src/shopping/ShoppingList.test.jsx` (new) has the component's first tests.

Household app SPA on React 18.3 (Vite, function components, served over HTTPS, so `crypto.randomUUID()` is available). Tests run with `npm test`: Vitest 2 with @testing-library/react 16 in jsdom. Existing and unchanged: `HouseholdPage.jsx` renders one shopping list per page, `<ShoppingList key={list.id} initialItems={list.items} />`, so a component instance always belongs to one list and `initialItems` is read once, on mount. `initialItems` is an array of at most 200 `{ id, name, done }` objects: `id` a unique non-empty string, `name` a non-empty string of at most 80 characters, `done` a boolean. The page's stylesheet already styles `.shopping-list` and its form controls.

Requirements

Files touched

Review this PR

React practice