Master the art of UI state management by adding a filter toggle to your movie app. Learn to control list rendering using boolean state and checkbox inputs.
Previously in this course, we explored Filtering the Movie List: Real-Time Search in React to manage text-based input. In this lesson, we’re leveling up our user interaction by adding a boolean toggle to filter our movie list.
When building complex UIs, you often need to let users switch between different "views" of data. Whether it's showing only "Available" items or filtering by a specific category, the pattern remains the same: use a checkbox or radio input to manage a boolean state, then use that state to derive what gets rendered on the screen.
A toggle is essentially a controlled input. Just like we learned in Building an Interactive Search Bar: Controlled Inputs in React, React needs to be the "source of truth" for the input's value.
For a checkbox, the "value" is its checked status—a boolean true or false. When the user clicks the checkbox, we update the state, which triggers a re-render. During that render, we filter the movie array based on the current state value.
Let’s add a "Show Only Action Movies" toggle to our movie browser. We’ll assume you’ve already set up your Dynamic Movie Cards: Building Reusable React Components.
JSXimport { useState } from CE9178">'react'; function MovieFilter({ movies }) { const [showOnlyAction, setShowOnlyAction] = useState(false); // 1. Derive the filtered list const filteredMovies = showOnlyAction ? movies.filter(movie => movie.genre === CE9178">'Action') : movies; return ( <div> <label> <input type="checkbox" checked={showOnlyAction} onChange={(e) => setShowOnlyAction(e.target.checked)} /> Show only Action movies </label> <div className="movie-list"> {filteredMovies.map(movie => ( <MovieCard key={movie.id} movie={movie} /> ))} </div> </div> ); }
useState(false) because the default view should show all movies.filteredMovies in state. We compute it during the render. This is a critical React pattern: if you can calculate data from existing props or state, don't create a new state variable for it.checked attribute forces the input to reflect our state, and onChange ensures our state stays in sync with the user's interaction.Your task is to extend the component above to support a second filter. Add a checkbox that filters the list to show only movies with a rating of 8.0 or higher.
Steps:
const [showTopRated, setShowTopRated] = useState(false);.filteredMovies logic to chain the filters:
JAVASCRIPTlet displayedMovies = movies; if (showOnlyAction) displayedMovies = displayedMovies.filter(...); if (showTopRated) displayedMovies = displayedMovies.filter(m => m.rating >= 8.0);
<label> and <input> to your JSX.showOnlyAction = true. Always use the setter function (setShowOnlyAction(true)) provided by useState.filteredMovies in state and update it inside the onChange handler. This creates a "syncing" nightmare. Always derive your filtered lists during the render cycle to keep your app predictable.<label> tag. It makes the text clickable, which is a massive improvement for user interaction and accessibility.We’ve successfully implemented a toggle filter by treating the checkbox as a controlled input. By managing a simple boolean state and deriving our view from that state, we keep our component logic clean and declarative. This pattern is the foundation for almost every interactive filter you will build in React.
Up next: We'll dive into the world of side effects and learn how to manage data outside the component lifecycle using useEffect.
Learn how to use the map method for list rendering in React. Master the key prop to keep your dynamic movie lists efficient, performant, and bug-free.
Building a Movie Filter Toggle
Polishing the UI
Finalizing the Movie Browser
Review of Component Lifecycle
Review of State Management
Building a Modal Component
Introduction to PropTypes
Performance Optimization Basics
Handling Browser History
Working with LocalStorage
Building a Favorites List
Handling Media in React
Introduction to Testing
Debugging React Apps
Deployment Basics
Using External Libraries
Advanced