What do these three dots in React do?

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

    The three dots in JavaScript are spread / rest operator.

    Spread operator

    The spread syntax allows an expression to be expanded in places where multiple arguments are expected.

    myFunction(...iterableObj);
    
    [...iterableObj, 4, 5, 6]
    
    [...Array(10)]
    

    Rest parameters

    The rest parameter syntax is used for functions with variable number of arguments.

    function(a, b, ...theArgs) {
      // ...
    }
    

    The spread / rest operator for arrays was introduced in ES6. There's a State 2 proposal for object spread / rest properties.

    TypeScript also supports the spread syntax and can transpile that into older versions of ECMAScript with minor issues.

提交回复
热议问题