Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 22 of the React Fundamentals: Build Modern UIs from Scratch course
ReactReactJune 25, 20263 min read

Building a Movie Filter Toggle in React: A Beginner's Guide

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.

ReactUIState ManagementComponentsHooksjavascriptfrontend

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.

Understanding the Toggle Pattern

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.

Implementing the Filter Toggle

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.

JSX
import { 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>
  );
}

Breaking Down the Logic

  1. State Initialization: We use useState(false) because the default view should show all movies.
  2. The Derived List: Notice we don't store 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.
  3. Controlled Input: The checked attribute forces the input to reflect our state, and onChange ensures our state stays in sync with the user's interaction.

Hands-On Exercise

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:

  1. Add a new state variable: const [showTopRated, setShowTopRated] = useState(false);.
  2. Update your filteredMovies logic to chain the filters:
    JAVASCRIPT
    let displayedMovies = movies;
    if (showOnlyAction) displayedMovies = displayedMovies.filter(...);
    if (showTopRated) displayedMovies = displayedMovies.filter(m => m.rating >= 8.0);
  3. Add a second <label> and <input> to your JSX.

Common Pitfalls

  • Mutating State Directly: Never do showOnlyAction = true. Always use the setter function (setShowOnlyAction(true)) provided by useState.
  • Over-complicating Derived State: Beginners often try to keep 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.
  • Ignoring Label Accessibility: Always wrap your checkbox in a <label> tag. It makes the text clickable, which is a massive improvement for user interaction and accessibility.

Recap

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.

Previous lessonEvent Bubbling and PropagationNext lesson Introduction to Side Effects
Back to Blog

Similar Posts

ReactReactJune 25, 20263 min read

Fetching Data from an API: A Practical React Guide

Master the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.

Read more
ReactReactJune 25, 20264 min read

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 22 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min

Rendering Lists of Data in React: A Practical Guide

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.

Read more
ReactReactJune 25, 20264 min read

Mastering useEffect Dependencies: Control Your React Lifecycle

Learn to master the useEffect dependency array to control exactly when your side effects run. Avoid infinite loops and optimize your React components today.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    3 min
  • 6

    Styling Components

    3 min
  • 7

    Passing Data with Props

    3 min
  • 8

    Dynamic Movie Cards

    3 min
  • 9

    Component Composition

    3 min
  • 10

    Conditional Rendering

    3 min
  • 11

    Rendering Lists of Data

    4 min
  • 12

    The Key Prop Explained

    4 min
  • 13

    Introduction to React State

    4 min
  • 14

    Managing State with useState

    3 min
  • 15

    Building an Interactive Search Bar

    3 min
  • 16

    Handling Click Events

    4 min
  • 17

    Updating State Based on Previous State

    4 min
  • 18

    Filtering the Movie List

    3 min
  • 19

    Handling Form Submissions

    3 min
  • 20

    Controlled Components

    4 min
  • 21

    Event Bubbling and Propagation

    3 min
  • 22

    Building a Movie Filter Toggle

    3 min
  • 23

    Introduction to Side Effects

    4 min
  • 24

    useEffect Dependencies

    4 min
  • 25

    Fetching Data from an API

    3 min
  • 26

    Handling Loading States

    3 min
  • 27

    Managing Errors

    3 min
  • 28

    Cleanup Functions in useEffect

    3 min
  • 29

    Debouncing Search Input

    3 min
  • 30

    Refactoring for Clean Code

    3 min
  • 31

    Folder Structure Best Practices

    4 min
  • 32

    Extracting Custom Hooks

    3 min
  • 33

    Prop Drilling and Context API

    3 min
  • 34

    Polishing the UI

    Coming soon
  • 35

    Finalizing the Movie Browser

    Coming soon
  • 36

    Review of Component Lifecycle

    Coming soon
  • 37

    Review of State Management

    Coming soon
  • 38

    Building a Modal Component

    Coming soon
  • 39

    Introduction to PropTypes

    Coming soon
  • 40

    Performance Optimization Basics

    Coming soon
  • 41

    Handling Browser History

    Coming soon
  • 42

    Working with LocalStorage

    Coming soon
  • 43

    Building a Favorites List

    Coming soon
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course