Students will create a simple React application where users can:
- Select a movie genre from a dropdown menu.
- See a list of movies based on the selected genre.
- View a loading message while the app "fetches" movie data.
- Display an error message if no genre is selected and the user tries to fetch data.
- Create a new React app.
- Create a component named
MovieSelector
.
Use useState
to manage the following state variables:
selectedGenre
(string
) – tracks the currently selected genre.isLoading
(boolean
) – indicates if the app is "loading" movie data.error
(string
) – stores any error messages.movies
(array
) – holds the list of movies for the selected genre.
- ✅ Create a dropdown menu to select a genre (e.g., Action, Comedy, Drama).
- ✅ Add a button labeled "Fetch Movies".
- ✅ When clicked:
- Show a loading spinner while "fetching" movie data.
- Display a list of movies after the data has been fetched.
- If no genre is selected, display an error message.
- The dropdown menu (
<select>
) should update theselectedGenre
state. - Practice managing controlled components in React.
-
Loading State:
Display"Loading movies..."
whenisLoading
istrue
. -
Error State:
Show an error message if the user tries to fetch data without selecting a genre. -
Movie List:
Display the movies list only after successful fetching.
Use the useState
hook to manage:
selectedGenre
isLoading
error
movies