Learn how to use conditional rendering in React to display UI elements dynamically. Master ternary operators and logical operators to build cleaner interfaces.
Previously in this course, we discussed Passing Data with Props and Dynamic Movie Cards, where we learned how to feed data into our components. Now, we'll take that data and make our UI smarter by implementing conditional rendering.
In professional development, you rarely show every element at all times. Perhaps a movie is missing a rating, or an image failed to load. Instead of breaking the UI, we use conditional rendering to show fallback states or hide elements entirely.
At its core, React is declarative. You tell React what the UI should look like based on the current state or props, rather than manually reaching into the DOM to hide or show elements. Because JSX is just JavaScript, we use standard language features—like the ternary operator and logical AND (&&)—to decide what gets rendered.
The ternary operator is the bread and butter of React conditionals. It’s perfect for "either-or" scenarios.
JSX// Syntax: condition ? (render if true) : (render if false) {isFeatured ? <Badge text="Featured" /> : null}
&&)Use the && operator when you want to show an element only if a condition is true, and show nothing if it's false.
JSX// Syntax: condition && (render if true) {hasDiscount && <p>Price: $9.99</p>}
Let's return to our movie-browser project. Currently, our MovieCard component displays data blindly. If a movie is missing a "release year" or has no "rating," we might end up with empty HTML tags or awkward layout gaps.
Let's update our MovieCard to handle these scenarios gracefully.
JSXfunction MovieCard({ title, year, rating, isPopular }) { return ( <div className="movie-card"> <h2>{title}</h2> {/* Conditionally render the year only if it exists */} {year && <p>Released: {year}</p>} {/* Use a ternary to show a special tag for popular movies */} <div className="status"> {isPopular ? ( <span className="badge-hot">Trending Now!</span> ) : ( <span className="badge-normal">Standard</span> )} </div> {/* Display rating only if it's greater than 0 */} {rating > 0 ? <p>Rating: {rating}/10</p> : <p>No ratings yet</p>} </div> ); }
By keeping our logic idempotent—ensuring that the same inputs always result in the same UI output—we avoid the common bugs associated with imperative DOM manipulation, as discussed in React Rendering: Why Your Logic Must Be Idempotent.
Open your project and locate your MovieCard component. Perform the following steps:
genre prop to your MovieCard.genre prop is provided.<small>Genre unknown</small> tag instead.count && <Component /> and count is 0, React will render the number 0 on your screen. This happens because 0 is a falsy value in JavaScript, but React treats it as a valid node to render. Always ensure your condition is a strict boolean (e.g., count > 0 && <Component />).Conditional rendering is how we make our components responsive to the data they receive. By leveraging the ternary operator for binary choices and the && operator for existence checks, we keep our UI clean and resilient. Remember: keep your logic simple, avoid rendering falsy numbers, and prioritize readability.
Up next: We'll take our dynamic MovieCard components to the next level by learning how to handle collections of data with Rendering Lists of Data.
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.
Conditional Rendering
Prop Drilling and Context API
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