Skip to content
Tutorials Center
My learning

Topics

React Reference ยท JSX Syntax and Rules

JSX spread attributes

Reference for JSX spread attributes: pass an object of props with {...obj}, how order decides which prop wins, forwarding rest props and the key caveat.

When you already hold an object of props, JSX lets you pass all of its entries at once with the spread syntax {...object} placed among the attributes. Each own, enumerable property of the object becomes a prop. <Avatar {...user} /> is the same as <Avatar name={user.name} size={user.size} /> for an object with those two keys.

The feature has two everyday uses. A wrapper component forwards the props it does not care about to an inner element (<button {...rest} />). A component builds up an object of props, for example from a configuration, and hands it over without listing every name.

JSX
function Button({ variant, ...rest }) {
  return <button className={'btn btn-' + variant} {...rest} />;
}

Try it

Order decides who wins
Forward the rest of the props

Syntax

JSX
<Component {...object} />
<Component before="a" {...object} after="b" />
const { used, ...rest } = props;   // collect the rest in a new object
Part Description
{...object} Copies own enumerable properties of object into the props. null, undefined, false and 0 spread to nothing.
Order Props are applied from left to right. A later attribute or spread replaces an earlier one with the same name.
...rest (in a parameter list) The matching object-rest syntax; it gathers the props you did not destructure by name.

Errors and edge cases: spreading is a shallow copy, so nested objects are shared, not cloned. The props object a component receives is read-only; build a new object when you need to change it.

Usage notes

  • Defaults first, overrides last. Put the spread of defaults before explicit attributes if callers should be able to override; put it after if you want the object to win (for example, props that must not be changed by the caller).
  • Do not spread blindly. {...props} also forwards props the target does not understand. On a DOM element, an unknown prop can become an invalid attribute, and React logs a warning. Destructure the props you own and spread only the rest.
  • Conditional props. Spread an object that is either filled or empty: {...(isAdmin ? { role: 'admin' } : {})}. Spreading false also works, so {...(isAdmin && { role: 'admin' })} is valid.
  • Beware of key. A key hidden inside a spread object makes React log a warning in development. Write it explicitly, before the spread: <Item key={item.id} {...item} />. The automatic JSX transform also handles a key that comes after a spread through the slower createElement path.
  • Do not spread children by accident. A spread object that includes children plus explicit JSX children: the JSX children win.
  • Readability. A component that only receives {...props} hides its interface. Prefer named props; use spread for real pass-through.
  • TypeScript. Spreading checks that the object's properties fit the target props type, which catches typos.