Movie Search App
What you'll build
A search box where the user types a movie title. As they type (debounced), the app fetches matching movies from an API and displays them as cards with title and year.
Goals
- Implement debounced search input
- Fetch search results from an API
- Render results as cards with map
- Handle loading and empty states
Prerequisites
- useEffect and useState
- Fetch API
- List rendering
Steps
- 1
Set up search state and debounce
Create state for the search query, results, and loading. Use a debounce timer in useEffect to delay the API call until the user stops typing.
Codeconst [query, setQuery] = useState(""); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { if (!query) { setResults([]); return; } const timer = setTimeout(async () => { setLoading(true); const res = await fetch(`https://www.omdbapi.com/?s=${query}&apikey=YOUR_KEY`); const data = await res.json(); setResults(data.Search || []); setLoading(false); }, 500); return () => clearTimeout(timer); }, [query]);Expected result
Typing triggers a debounced search after 500ms of inactivity.
- 2
Render the search input and results
Show a search input bound to query state, and map over results to display movie cards.
Code<input value={query} onChange={e => setQuery(e.target.value)} placeholder="Search movies..." /> {loading && <p>Searching...</p>} {results.map(movie => ( <div key={movie.imdbID}> <h3>{movie.Title}</h3> <p>{movie.Year}</p> </div> ))}Expected result
Movie results appear as cards with title and year.
- 3
Add state management & event handling
Use useState to manage component state and add onClick/onChange handlers to interactive elements. State is the heart of React components, and event handling lets them respond to user actions.
Codeimport { useState } from "react"; export function MyComponent() { const [count, setCount] = useState(0); const [text, setText] = useState(""); return ( <div> <button onClick={() => setCount(c => c + 1)}> Count: {count} </button> <input value={text} onChange={(e) => setText(e.target.value)} /> </div> ); }Expected result
Clicking the button increments the count; the input content updates in real time as you type.
- 4
Add Props validation & component split
Define a Props interface (TypeScript) or propTypes for the component, and split large components into smaller sub-components. Type-safe Props make the component interface clear and reusable.
Codeinterface ChildProps { label: string; value: number; onChange: (v: number) => void; } function Child({ label, value, onChange }: ChildProps) { return ( <label> {label} <input type="number" value={value} onChange={(e) => onChange(Number(e.target.value))} /> </label> ); }Expected result
The child component receives type-safe Props; the parent can reuse it to render multiple fields.
- 5
Test component rendering
Use React Testing Library to render the component and assert output, verifying state updates correctly after interaction. Component tests ensure UI behavior matches expectations.
Codeimport { render, screen, fireEvent } from "@testing-library/react"; test("button increments count", () => { render(<MyComponent />); const btn = screen.getByText(/Count:/); fireEvent.click(btn); expect(screen.getByText("Count: 1")).toBeInTheDocument(); });Expected result
Test passes: after clicking, the button text becomes 'Count: 1'.
Try it yourself
What you learned
You learned debounced API calls with useEffect and setTimeout cleanup, a common pattern for search inputs. This prevents excessive API requests while the user is still typing.
Related Resources
Related Lessons
- Fetching Data
API calls in useEffect.
- useEffect
Debounce with cleanup.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.