What do these three dots in React do?

前端 未结 29 2583
不思量自难忘°
不思量自难忘° 2020-11-21 23:53

What does the ... do in this React (using JSX) code and what is it called?



        
相关标签:
29条回答
  • 2020-11-22 00:34

    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.

    0 讨论(0)
  • 2020-11-22 00:36

    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.

    • Merge two arrays

    var arr1 = [1,2,3];
    var arr2 = [4,5,6];
    
    arr1 = [...arr1, ...arr2];
    console.log(arr1);  //[1, 2, 3, 4, 5, 6]

    • Copying array:

    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).

    • Add values of one array to other at specific index e.g 3:

    var arr1 = [4,5]
    var arr2 = [1,2,3,...arr1,6]
    console.log(arr2);	// [1, 2, 3, 4, 5, 6]

    • When calling a constructor with new:

    var dateFields = [1970, 0, 1];  // 1 Jan 1970
    var d = new Date(...dateFields);
    
    console.log(d);

    • Spread in object literals:

    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

    • As a rest parameter syntax which allows us to represent an indefinite number of arguments as an array:

    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

    0 讨论(0)
  • 2020-11-22 00:36

    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

    0 讨论(0)
  • 2020-11-22 00:37

    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.

    0 讨论(0)
  • 2020-11-22 00:37
    const Profile =  {
              firstName: "kazi",
              lastName: "ahsan"
       }
    
    const ProfileUpdate =  {
              firstName: "kazi",
              lastName: "ahsan"
     }
    
    
    const newProfile = {...Profile, ...ProfileUpdate}
    

    Hope this helps someone.

    0 讨论(0)
提交回复
热议问题