What does the ...
do in this React (using JSX) code and what is it called?
It is called spreads syntax in javascript.
It use for destructuring an array or object in javascript.
example:
const objA = { a: 1, b: 2, c: 3 }
const objB = { ...objA, d: 1 }
/* result of objB will be { a: 1, b: 2, c: 3, d: 1 } */
console.log(objB)
const objC = { ....objA, a: 3 }
/* result of objC will be { a: 3, b: 2, c: 3, d: 1 } */
console.log(objC)
You can do it same result with Object.assign()
function in javascript.
Reference:Spread syntax
It is common practice to pass props around in a React application. In doing this we able to apply state changes to the child component regardless of whether it is Pure or Impure (stateless or stateful). There are times when the best approach, when passing in props, is to pass in singular properties or an entire object of properties. With the support for arrays in ES6 we were given the "..." notation and with this we are now able to achieve passing an entire object to a child.
The typical process of passing props to a child is noted with this syntax:
var component = <Component foo={x} bar={y} />;
This is fine to use when the number of props is minimal but becomes unmanageable when the prop numbers get too much higher. A problem with this method occurs when you do not know the properties needed within a child component and the typical JavaScript method is to simple set those properties and bind to the object later. This causes issues with propType checking and cryptic stack trace errors that are not helpful and cause delays in debugging. The following is an example of this practice, and what not to do:
var component = <Component />;
component.props.foo = x; // bad
component.props.bar = y;
This same result can be achieved but with more appropriate success by doing this:
var props = {};
props.foo = x;
props.bar = y;
var component = Component(props); // Where did my JSX go?
But does not use JSX spread or JSX so to loop this back into the equation we can now do something like this:
var props = {};
props.foo = x;
props.bar = y;
var component = <Component {...props} />;
The properties included in "...props" are foo: x, bar: y. This can be combined with other attributes to override the properties of "...props" using this syntax:
var props = { foo: 'default' };
var component = <Component {...props} foo={'override'} />;
console.log(component.props.foo); // 'override'
In addition we can copy other property objects onto each other or combine them in this manner:
var oldObj = { foo: 'hello', bar: 'world' };
var newObj = { ...oldObj, foo: 'hi' };
console.log(newObj.foo); // 'hi';
console.log(newObj.bar); // 'world';
Or merge two different objects like this (this is not yet available in all react versions):
var ab = { ...a, ...b }; // merge(a, b)
Another way of explaining this, according to Facebook's react/docs site is:
If you already have "props" as an object, and you want to pass it in JSX, you can use "..." as a SPREAD operator to pass the whole props object. The following two examples are equivalent:
function App1() {
return <Greeting firstName="Ben" lastName="Hector" />;
}
function App2() {
const props = {firstName: 'Ben', lastName: 'Hector'};
return <Greeting {...props} />;
}
Spread attributes can be useful when you are building generic containers. However, they can also make your code messy by making it easy to pass a lot of irrelevant props to components that don't care about them. This syntax should be used sparingly.
if you have an array of elements and you want to display the elements you just use ...arrayemaments and it will iterate over all the elements
The meaning of ... depends on where you use it in the code,
const numbers = [1,2,3];
const newNumbers = [...numbers, 4];
console.log(newNumbers) //prints [1,2,3,4]
const person = {
name: 'Max'
};
const newPerson = {...person, age:28};
console.log(newPerson); //prints {name:'Max', age:28}
const filter = (...args) => {
return args.filter(el => el ===1);
}
console.log(filter(1,2,3)); //prints [1]
Is usually called spread operator, it is use to expand wherever is required
example
const SomeStyle = {
margin:10,
background:#somehexa
}
you can use this where ever you requires it more about spread operator Spread syntax.
In a short, the three dots ...
is a spread operator in ES6(ES2015). Spread operator will fetch all the data.
let a = [1, 2, 3, 4];
let b = [...a, 4, 5, 6];
let c = [7,8,...a];
console.log(b);
Will give the result [1,2,3,4,5,6]
console.log(c);
Will give the result [7,8,1,2,3,4]