Learn to build a professional modal component in React. Master state-driven visibility, overlay patterns, and click-outside event handling for your movie app.
Previously in this course, we explored review of state management to understand how data flows through our application. In this lesson, we’ll take that knowledge and apply it to a specific UI challenge: displaying detailed information in a modal.
A modal is a focused overlay that interrupts the user's flow to present critical content. Because it sits on top of your existing dynamic movie cards, it requires careful management of state and events.
At its core, a modal is just a component that conditionally renders based on a boolean state. However, a "true" modal behaves differently than a standard block element. It needs an overlay to dim the background, a container to hold the content, and a way to dismiss itself when the user clicks outside.
To control the modal, we lift the visibility state to the parent component that contains our movie list. When a user clicks a "Details" button, we set that state to true.
JSX// MovieList.jsx import { useState } from CE9178">'react'; import Modal from CE9178">'./Modal'; export default function MovieList({ movies }) { const [selectedMovie, setSelectedMovie] = useState(null); return ( <div> {movies.map(movie => ( <div key={movie.id}> <h3>{movie.title}</h3> <button onClick={() => setSelectedMovie(movie)}>View Details</button> </div> ))} {selectedMovie && ( <Modal onClose={() => setSelectedMovie(null)} movie={selectedMovie} /> )} </div> ); }
By storing the selectedMovie object itself in state (rather than just a boolean), we effectively handle both the "open" state and the data context for the modal simultaneously.
Now, let's build the Modal component. We want a full-screen overlay that captures clicks to close the window.
JSX// Modal.jsx export default function Modal({ onClose, movie }) { return ( <div className="modal-overlay" onClick={onClose}> <div className="modal-content" onClick={(e) => e.stopPropagation()}> <h2>{movie.title}</h2> <p>{movie.description}</p> <button onClick={onClose}>Close</button> </div> </div> ); }
Notice the e.stopPropagation() call on the modal-content div. Without this, clicking on the modal content would trigger the onClick event of the modal-overlay behind it, causing the modal to close immediately. This is a classic example of handling click events where we need to stop the event from bubbling up to parent elements.
In your movie browser project, add a "Details" button to each movie card.
Modal.jsx file.position: fixed and background: rgba(0,0,0,0.7) in your CSS).onClose logic so that clicking the overlay or a "Close" button removes the modal from the DOM.e.stopPropagation() on your inner container.modal-overlay has a high z-index value.We’ve successfully extended our creating static components foundation to include interactive overlays. By using state to track the active movie and carefully managing click events, we’ve created a seamless viewing experience. Remember that modals are just components that exist conditionally; they don't require complex routing, just clean state management.
Up next: We will dive into PropTypes to add type safety and catch bugs in our component props before they happen.
Master the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.
Building a Modal Component
Handling Media in React
Introduction to Testing
Debugging React Apps
Deployment Basics
Using External Libraries
Advanced