Course contentsShow
← React
Lesson 242 of 2,3967. Props FundamentalsFree lesson

Rest Parameters for Remaining Props

Use the rest operator (...rest) to collect all undestructured props into a single object for flexibility.

Rest Parameters for Remaining Props

What you'll learn: How to use the rest operator (...rest) to collect leftover props into a single object for flexible component APIs.

The Core Idea

Sometimes you want to destructure a few specific props by name, but also collect all the other props someone might pass to your component. The rest operator (...rest) lets you do exactly that — it gathers all the props you didn't explicitly destructure into a new object.

Think of it like sorting mail: you pull out the letters addressed specifically to you, then bundle everything else into a pile labeled "rest" to pass along or handle later.

Basic Syntax

function Button({ variant, children, ...rest }) {
  // variant and children are extracted
  // everything else is in the 'rest' object
  console.log(rest); // { onClick: fn, disabled: true, ... }
  
  return <button className={variant}>{children}</button>;
}

Why This Matters

This pattern shines when you want to forward unknown props to child elements:

function Input({ label, ...rest }) {
  return (
    <div>
      <label>{label}</label>
      <input {...rest} />
    </div>
  );
}

// Usage: all extra props go straight to the <input>
<Input label="Name" type="text" placeholder="Enter name" disabled />

You extracted label for your wrapper logic, but type, placeholder, and disabled automatically land on the actual input element through ...rest.

The variable name doesn't have to be rest — you could use ...otherProps or ...remaining — but rest is conventional.

Key Takeaway: Use ...rest when destructuring props to collect all undestructured props into an object, enabling flexible component APIs that can forward arbitrary props to child elements.