What do these three dots in React do?

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

    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

提交回复
热议问题