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

Passing JSX Elements as Props

Master the technique of treating JSX as first-class values that can be passed to any prop.

Passing JSX Elements as Props

What you'll learn: How to treat JSX like any other JavaScript value and pass it directly to components through props.

The Core Idea

In React, JSX isn't magic—it's just JavaScript. This means you can pass JSX elements as prop values just like you'd pass strings, numbers, or functions. Instead of always using the special children prop, you can create custom prop names that accept JSX, giving you precise control over where different pieces of UI appear in your component.

Think of it like a template with labeled blanks: instead of one generic "content goes here" slot (children), you have named slots like "header goes here" and "footer goes here." Each prop becomes a portal for JSX.

Why This Matters

When building layouts or components with multiple content areas, you can't rely solely on children. Consider a card component that needs a title, body, and actions section. You could pass JSX to each area:

<Card
  title={<h2>Welcome</h2>}
  body={<p>This is the content</p>}
  actions={<button>Click me</button>}
/>

Inside Card, you simply render those props wherever they belong:

function Card({ title, body, actions }) {
  return (
    <div className="card">
      <div className="card-header">{title}</div>
      <div className="card-body">{body}</div>
      <div className="card-footer">{actions}</div>
    </div>
  );
}

The component receives JSX elements and renders them in specific locations. No strings, no special syntax—just props containing JSX.

When to Use This Pattern

Use JSX props when you need multiple distinct content areas with clear semantic meaning. It makes the API explicit: consumers know exactly what goes where.

Key Takeaway: JSX elements are first-class values in JavaScript—pass them as props to any component, creating flexible, multi-slot layouts without relying solely on children.