Extracting Known Props with Rest Syntax
What you'll learn: How to cleanly separate props your component uses from props it should pass down to child elements.
The Problem We're Solving
You've learned how to spread props onto elements, but often you need some props for your own component logic while passing the rest down. Grabbing everything and spreading it means your component might use props it shouldn't, or pass down props that child elements don't understand.
The Rest Syntax Solution
Using destructuring with the rest operator (...rest), you can extract specific props by name and collect everything else into a separate object:
function Button({ variant, size, ...rest }) {
// 'variant' and 'size' are extracted for this component
// 'rest' contains ALL other props (onClick, className, etc.)
const buttonClass = `btn btn-${variant} btn-${size}`;
return <button className={buttonClass} {...rest} />;
}
// Usage:
<Button
variant="primary"
size="large"
onClick={handleClick}
disabled
aria-label="Submit"
/>
// Button uses variant & size, spreads the rest to <button>
Why This Matters
This pattern lets you create wrapper components that enhance native elements without blocking their standard functionality. Your Button component can apply custom styling based on variant and size, while still accepting any valid button attributes (onClick, disabled, type, etc.) without explicitly listing them all.
Key Takeaway: Use destructuring with ...rest to pull out component-specific props by name, then spread the remaining props to child elements—keeping your component API flexible while maintaining clear internal logic.