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 10 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20263 min read

Conditional Rendering: Mastering UI Logic in React

Learn how to use conditional rendering in React to display UI elements dynamically. Master ternary operators and logical operators to build cleaner interfaces.

Reactfrontendweb developmentJSXjavascript

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.

Understanding UI Logic in JSX

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.

1. The Ternary Operator

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}

2. The Logical AND (&&)

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>}

Worked Example: Enhancing the MovieCard

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.

JSX
function 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.

Hands-on Exercise

Open your project and locate your MovieCard component. Perform the following steps:

  1. Add a genre prop to your MovieCard.
  2. Update the JSX to render the genre only if the genre prop is provided.
  3. If the genre is missing, render a small <small>Genre unknown</small> tag instead.
  4. Test it by passing a movie object with the genre and one without.

Common Pitfalls

  • The "0" Bug: If you use 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 />).
  • Over-nesting: Avoid nesting multiple ternary operators inside each other. It becomes unreadable very quickly. If your logic is complex, it’s often cleaner to extract it into a helper function or a separate small component.
  • Side Effects in JSX: Never perform data fetching or state updates directly inside your JSX conditional blocks. JSX is strictly for describing the UI. For more on managing complex logic, see React Conditional Rendering: Mastering Guard Clauses and Ternary Chains.

Recap

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.

Previous lessonComponent CompositionNext lesson Rendering Lists of Data
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, 20263 min read

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 10 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

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.

Read more
ReactReactJune 25, 20264 min read

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
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

    Coming soon
  • 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