Skip to content
Tutorials Center
My learning

Topics

Lesson 115 of 132 ยท Projects

Project: Movie Search App

Build a movie search app in React - a debounced search box, loading and empty states, race-safe results and a details view - using a simulated search API that runs in your browser.

Search-as-you-type is everywhere, and it hides three classic problems. Firing a request on every keystroke wastes work, so you debounce. Responses can arrive out of order, so you must ignore stale ones. And the screen has at least four states: idle, loading, results and empty. This project builds all of that around a movie catalogue.

There is no network in the sandbox, so the "API" is a small function that filters a fixed list of made-up films after a short setTimeout delay. The component code is exactly what you would write against a real fetch; only searchMovies would change. See data fetching and effect cleanup for the underlying ideas.

Stage 1: a debounce hook

A debounced value follows another value but only after it has stopped changing for a while. A custom hook wraps this (custom hooks). Each change restarts a timer, and the cleanup cancels the previous timer.

Stage 1: useDebouncedValue

Type fast and watch the second line wait until you pause. That pause is when a real app would send its request.

Stage 2: search with loading, empty and race safety

Now connect the debounced text to a request. The stand-in API takes longer for shorter queries, which deliberately makes responses arrive out of order; the ignore flag keeps the screen honest.

Stage 2: debounced search

One status field replaces several booleans, so impossible combinations (loading and empty at once) cannot happen. Try "light", then "lighth": each shows only its own results.

Stage 3: a details view

Selecting a result shows details. Store only the selected id, not a copy of the movie, so the list stays the single source of truth (choosing state structure). A button inside each item is keyboard accessible by default.

Stage 3: list and details

Because selected is looked up from the current results, filtering the selected movie out of the list clears the details automatically: no extra code, no stale copy.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What does debouncing a search box achieve?

    Each keystroke restarts a timer; the value only updates after the delay passes without a new change, so fewer requests are made.
  2. 2. Why does the search effect return a cleanup that sets ignore = true?

    Requests can finish out of order. Marking an earlier request as ignored means only the latest one may update state.
  3. 3. Why store selectedId rather than the selected movie object?

    Looking the movie up by id from current data avoids a duplicate copy that could drift out of date.

Summary

  • Debounce typed input with a small custom hook built on a timer and its cleanup.
  • Model the request as one status plus data, and ignore stale responses in the effect cleanup.
  • Store ids, derive the selected item.
  • The search API here is a stand-in; swap searchMovies for a real fetch and keep everything else.

Next: Project: Music Player