What do these three dots in React do?

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

    For someone who wants to understand this simple and fast:

    First of all, this is not a syntax only to react. this is a syntax from ES6 called Spread syntax which iterate(merge, add..etc) array and object. read more about here

    So answer to the question: let's imagine you have this tag:

    and You do this:

    const user = {
      "name"=>"Joe",
      "age"=>"50"      
      "test"=>"test-val"
    };
    
    
    

    then the tag will equal this:

    
    

    So what happened was when you use Spread syntax in a react tag it takes tag's attribute as object attributes which merge(replace if it exists) with the given object user. also, you might have noticed one thing that it only replaces before attribute, not after attributes. so in this example age remains as it is.

    Hopes this helps :)

提交回复
热议问题