Course contentsShow
← React
Lesson 261 of 2,3968. Props PatternsFree lesson

Explicit Props Before Spread

How prop order matters: explicit props override spread values, enabling safe defaults with consumer customization.

Explicit Props Before Spread

What you'll learn: How placing explicit props after the spread operator lets you safely override defaults while still passing all other props through.

The Core Idea

In the last lesson, you learned about spreading props onto components. Now here's the crucial detail: the order matters. When you spread props and also define explicit props, whichever comes last wins.

This creates a powerful pattern: spread first to accept all incoming props, then add explicit props afterward to set safe defaults or enforce specific values.

// Bad: spread AFTER explicit props
// The spread will overwrite your explicit value
function Button(props) {
  return <button type="button" {...props} />;
  // If props contains type="submit", your type="button" is lost!
}

// Good: explicit props AFTER spread
function Button(props) {
  return <button {...props} type="button" />;
  // Now type="button" always wins unless consumer passes type
}

Why This Works

Think of it like stacking transparent sheets of paper with text. Each prop assignment is a new sheet. You read from the top down, so the last sheet defines what you see.

When you write {...props} type="button", React processes left to right:

  1. First, spread all props from the object
  2. Then, set type="button", which overwrites any type that came from the spread

This lets consumers customize everything except what you explicitly override after the spread.

// Component author's perspective
function Card(props) {
  return <div {...props} className="card" />;
  // className will ALWAYS be "card", regardless of props
}

// Consumer perspective
<Card style={{ margin: 10 }} /> // style passes through, className forced to "card"

Key Takeaway: Place explicit props after the spread operator to create overridable defaults or enforce non-negotiable values—last prop wins.