Edit on GitHub

JSX Spread Attributes

If you know all the properties that you want to place on a component ahead of time, it is easy to use JSX:

  var component = <Component foo={x} bar={y} />;

Mutating Props is Bad #

If you don't know which properties you want to set, you might be tempted to add them onto the object later:

  var component = <Component />;
  component.props.foo = x; // bad
  component.props.bar = y; // also bad

This is an anti-pattern because it means that we can't help you check the right propTypes until way later. This means that your propTypes errors end up with a cryptic stack trace.

The props should be considered immutable. Mutating the props object somewhere else could cause unexpected consequences so ideally it would be a frozen object at this point.

Spread Attributes #

Now you can use a new feature of JSX called spread attributes:

  var props = {};
  props.foo = x;
  props.bar = y;
  var component = <Component {...props} />;

The properties of the object that you pass in are copied onto the component's props.

You can use this multiple times or combine it with other attributes. The specification order is important. Later attributes override previous ones.

  var props = { foo: 'default' };
  var component = <Component {...props} foo={'override'} />;
  console.log(component.props.foo); // 'override'

What's with the weird ... notation? #

The ... operator (or spread operator) is already supported for arrays in ES6. There is also an ECMAScript proposal for Object Rest and Spread Properties. We're taking advantage of these supported and developing standards in order to provide a cleaner syntax in JSX.