Course contentsShow
← React
Lesson 1143 of 2,39629. Composition: SlotsFree lesson

Conditional Rendering of Prop Content

Render JSX props conditionally based on component state or other props, treating them as first-class conditional values.

Conditional Rendering of Prop Content

What you'll learn: How to treat JSX props as first-class values that can be rendered conditionally based on component state or other props.

The Core Idea

Since JSX props are just objects (as you learned earlier), you can apply all the same conditional rendering techniques to them that you use with any other data. This means you can decide whether to render a slot's content based on state, other props, boolean flags, or any logical condition.

Think of it like a restaurant menu board with optional sections: the "specials" section only appears when there are specials to show. Your component slots work the same way—they can appear, disappear, or change based on runtime conditions.

How It Works

You might receive multiple content props (like header, sidebar, footer) and decide which ones to render based on:

  • Component state: Show errorMessage prop only when hasError is true
  • Prop conditions: Render sidebar only if showSidebar prop is true
  • Content existence: Display footer only if it was actually passed
  • Combinations: Show banner when both isLoggedIn and hasBanner are true

The pattern is straightforward: wrap your slot rendering in conditional logic just like you would with any JSX. Use ternaries, && operators, or early returns—whichever makes your intent clearest.

Why This Matters

This pattern gives you fine-grained control over layout composition without forcing parent components to handle the conditional logic themselves. The component that owns the layout structure also controls when each piece appears, keeping concerns properly separated.

Key Takeaway: JSX props can be conditionally rendered using standard JavaScript logic, letting components control complex layout behaviors while keeping content definition flexible.