The search box
The answer to the exercise in The search box.
The form's default is to send itself and reload. Call
event.preventDefault() first, then onSearch(). The three cards stay,
and the line can show what you typed.
import { useState } from "react"; import "./styles.css"; import SearchForm from "./SearchForm"; import ShowList from "./ShowList"; const start = [ { id: "s1", title: "The Bear", year: 2022, genre: "Drama", genres: ["Drama"], rating: 8.6, still: "still-warm", watched: false }, { id: "s2", title: "Severance", year: 2022, genre: "Thriller", genres: ["Thriller"], rating: 8.7, still: "still-cold", watched: false }, { id: "s3", title: "Andor", year: 2022, genre: "Sci-Fi", genres: ["Sci-Fi"], rating: 8.4, still: "still-dusty", watched: true }, ]; export default function App() { const [shows, setShows] = useState(start); const [query, setQuery] = useState(""); const [submittedQuery, setSubmittedQuery] = useState(""); function handleToggle(id) { setShows( shows.map((show) => show.id === id ? { ...show, watched: !show.watched } : show ) ); } function handleSearch() { setSubmittedQuery(query); } const watched = shows.filter((show) => show.watched).length; return ( <div className="page"> <header className="header"> <h1>My watchlist</h1> <p className="count"> {shows.length} shows · {watched} watched </p> </header> <SearchForm query={query} onQueryChange={setQuery} onSearch={handleSearch} /> {submittedQuery ? ( <p className="status">Looking for: {submittedQuery}</p> ) : null} <ShowList shows={shows} onToggle={handleToggle} /> </div> ); }
Resize editor and preview
Resize sandbox height