Movie List With Sort and Search
Build a small UI that lists movies loaded from a JSON endpoint. The starter's api.js provides fetchMovies(), a stand-in for fetching db.json: it resolves with an array of { id, name, year } after a short delay, and fetchMovies({ fail: true }) rejects, so you can see the error path.
Requirements
- Load the movies when the component mounts. Show
Loading movies…while the request is in flight and an error message if it fails. - Display each movie's name and year as a table row.
- The search box filters by name: case-insensitive, matching anywhere in the name. Show
No movies match.when nothing does. - The Name and Year headers sort the list. Clicking a header sorts ascending; clicking it again flips to descending. Start sorted by name ascending, and keep
aria-sorton the active header (ascendingordescending,noneelsewhere). - Keep the starter's markup: the search box labelled
Search movies, header buttons namedNameandYear, and one<tr>per movie in the body.
Be ready to explain how the component would cope with thousands of movies.
Hints
Hint 1
Three pieces of state: the loaded movies, a status (loading, ready, error), and the query and sort. Everything shown is derived from them, so nothing is stored twice.
Hint 2
Fetch in a useEffect with an empty dependency list, and ignore a result that arrives after the component unmounted (a cancelled flag in the cleanup).
Hint 3
Sort a copy: [...filtered].sort(...). Sorting in place mutates the loaded array and breaks the next filter.
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.