Two-way binding
Form fields bind both ways on their own. value={ query } on an input shows query, and typing writes it back: the view stores it as local state and runs data() again, as setData() plus refresh() would. No input handler to write.
- A bare name,
value={ query }, writes local state. - A record path,
checked={ todo.done }, writes the record throughupdate(), rules checked. - Text inputs and textareas write as you type; numbers, checkboxes, dates and selects write on change.
Anything that is not a plain name or path, like value={ query.trim() }, shows the value but never writes it.
Your turn
- Add a search box above the genre chips:
<input class="search" type="search" placeholder="Search titles and artists" aria-label="Search" value={ query }>
- In
data(), read the query back, return it untouched asquery, and keep only albums whose title or artist contains its trimmed, lowercased form:
const raw = this.getData().query ?? '';
const needle = raw.trim().toLowerCase();
const matches = (album) =>
album.title.toLowerCase().includes(needle) || album.artist.toLowerCase().includes(needle);
Return query: raw, not needle: whatever data() returns as query is what the box shows, so returning the cleaned-up value would rewrite the field under your cursor on every key.
- Combine it with the genre filter, then type into the box. The grid and its count follow every key.
That is views. Chapter 6 breaks the shop into components.