React Passing down props using spread operator


Instead of

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

Where each property needs to be passed as a single prop value you could use the spread operator ... supported for arrays in ES6 to pass down all your values. The component will now look like this.

var component = <Component {...props} />;

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

The order is important. Later attributes override previous ones.

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

Another case is that you also can use spread operator to pass only parts of props to children components, then you can use destructuring syntax from props again.

It's very useful when children components need lots of props but not want pass them one by one.

 const { foo, bar, other } = this.props // { foo: 'foo', bar: 'bar', other: 'other' };
 var component = <Component {...{foo, bar}} />;
 const { foo, bar } = component.props
 console.log(foo, bar); // 'foo bar'