What does the ...
do in this React (using JSX) code and what is it called?
This a spread operator...
For example if you have an array first=[1,2,3,4,5]
and another second=[6,7,8]
.
[...first, ...second] //result is [1,2,3,4,5,6,7,8]
The same can also be done with json objects.
Three dots ...
represents Spread Operators or Rest Parameters,
It allows an array expression or string or anything which can be iterating to be expanded in places where zero or more arguments for function calls or elements for array are expected.
var arr1 = [1,2,3];
var arr2 = [4,5,6];
arr1 = [...arr1, ...arr2];
console.log(arr1); //[1, 2, 3, 4, 5, 6]
var arr = [1, 2, 3];
var arr2 = [...arr];
console.log(arr); //[1, 2, 3]
Note: Spread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays as the following example shows (it's the same with Object.assign() and spread syntax).
var arr1 = [4,5]
var arr2 = [1,2,3,...arr1,6]
console.log(arr2); // [1, 2, 3, 4, 5, 6]
var dateFields = [1970, 0, 1]; // 1 Jan 1970
var d = new Date(...dateFields);
console.log(d);
var obj1 = { foo: 'bar', x: 42 };
var obj2 = { foo: 'baz', y: 13 };
var clonedObj = { ...obj1 };
console.log(clonedObj); //{foo: "bar", x: 42}
var mergedObj = { ...obj1, ...obj2 };
console.log(mergedObj); //{foo: "baz", x: 42, y: 13}
Note that foo
property of obj1 has been overwritten by obj2 foo
property
function sum(...theArgs) {
return theArgs.reduce((previous, current) => {
return previous + current;
});
}
console.log(sum(1, 2, 3)); //6
console.log(sum(1, 2, 3, 4)); //10
Note:Spread syntax (other than in the case of spread properties) can be applied only to iterable objects: So following will throw error
var obj = {'key1': 'value1'};
var array = [...obj]; // TypeError: obj is not iterable
Reference1
Reference2
The three dots (...)
are called the spread operator, and this is conceptually similar to the ES6 array spread operator, JSX
taking advantage of these supported and developing standards in order to provide a cleaner syntax in JSX
Spread properties in object initializers copies own enumerable properties from a provided object onto the newly created object.
let n = { x, y, ...z }; n; // { x: 1, y: 2, a: 3, b: 4 }
Reference:
1) https://github.com/sebmarkbage/ecmascript-rest-spread#spread-properties
2) https://facebook.github.io/react/docs/jsx-spread.html
As you know ...
are called Spread Attributes which the name represents it allows an expression to be expanded.
var parts = ['two', 'three'];
var numbers = ['one', ...parts, 'four', 'five']; // ["one", "two", "three", "four", "five"]
And in this case(I'm gonna simplify it).
//just assume we have an object like this:
var person= {
name: 'Alex',
age: 35
}
This:
<Modal {...person} title='Modal heading' animation={false} />
is equal to
<Modal name={person.name} age={person.age} title='Modal heading' animation={false} />
So in short, it's a neat short-cut, we can say.
const Profile = {
firstName: "kazi",
lastName: "ahsan"
}
const ProfileUpdate = {
firstName: "kazi",
lastName: "ahsan"
}
const newProfile = {...Profile, ...ProfileUpdate}
Hope this helps someone.