Given this example code from the React docs:
var props = {};
props.foo = x;
props.bar = y;
var component = <Component {...props} />;
I did some looking into what ...props
actually evaluates to, which is this:
React.__spread({}, props)
Which in turn evaluates to {foo: x, bar: y}
.
But what I'm wondering is, why can't I just do this:
var component = <Component props />;
I don't see understand what the point of the spread operator is.
One of the best overviews of how object-rest-spread syntax works with react is published at reactpatterns.com:
-- quoted from reactpatterns.com by @chantastic
Another good overview was published on the babeljs blog post React on ES6+ by Steven Luscher:
-- quoted from "BabelJS.org blog - React on ES6+" by Steven Luscher
This helps make your code more succinct - since
props
is an object, the spread operator takes the properties of the object you pass in and applied them to the component. So the Component will have properties afoo
with a value ofx
and abar
with a value ofy
.It would be the same as:
just shorter