What do these three dots in React do?

前端 未结 29 2594
不思量自难忘°
不思量自难忘° 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:28

    spread operator(triple operator) introduce in ecama script 6(ES6).Ecama script(ES6) is a wrapper of javascript.

    spread operator enumerable properties in props. this.props = { firstName: 'Dan', lastName: 'Abramov', city: 'New York', country: 'USA' }

    {...this.props} = { firstName: 'Dan', lastName: 'Abramov', city: 'New York', country: 'USA' }

    But the main feature spread operator is used for a reference type.

    For example
    let person= {
        name: 'Alex',
        age: 35 
    }
    person1= person;
    
    person1.name = "Raheel";
    
    console.log( person.name); // output: Raheel
    

    This is called reference type, one object affects other objects because they are shareable in memory. If you getting value independently mean spread memory both use spread operator.

     let person= {
            name: 'Alex',
            age: 35 
        }
    person2 = {...person};
    
    person2.name = "Shahzad";
    
    console.log(person.name); // output: Alex
    

提交回复
热议问题