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

Slot Props with Default Values

Providing default JSX for slots when consumers don't supply their own content using default parameters.

Slot Props with Default Values

What you'll learn: How to provide fallback JSX for your slot props so components look complete even when consumers don't pass custom content.

The Problem: Empty Slots Look Broken

When you design components with multiple slots (like header, footer, sidebar), users might only fill some of them. Without defaults, empty slots can leave awkward gaps or broken layouts.

Adding Default Values with Parameter Defaults

Just like regular function parameters, you can provide default JSX for slot props:

function Card({ 
  header = <h3>Untitled</h3>,
  content,
  footer = <small>No footer provided</small>
}) {
  return (
    <div className="card">
      <div className="card-header">{header}</div>
      <div className="card-body">{content}</div>
      <div className="card-footer">{footer}</div>
    </div>
  );
}

// Using it:
<Card content={<p>Just the content!</p>} />
// Renders with default header and footer

<Card 
  header={<h2>Custom Title</h2>}
  content={<p>My content</p>}
  footer={<button>Action</button>}
/>
// All slots customized

Why This Matters

Default slot values give your components sensible fallbacks. It's like a restaurant menu with "chef's choice" options—if a customer doesn't specify, they still get something good. Your component works out-of-the-box but remains flexible.

You can even use null as a default to hide a section entirely: footer = null means "no footer by default unless provided."

Key Takeaway: Default parameters on slot props provide fallback JSX, ensuring your components render gracefully even when consumers only customize some slots—balancing flexibility with usability.