What is the purpose of the distribution element here?

I study the reaction from a book, and in the example, the author made a rendering component this way

return (
        <div className="star-rating">
        {[...Array(totalStars)].map((n,i)=>
            <Star key = {i} selected={i<starSelected} onClick={()=>this.change(i+1)} />
        )}
        <p>{starsSelected} of  {totalStars} stars </p>+
    )

What is the purpose of the distribution element when initializing the array?

+4
source share
2 answers

Array(totalStars)creates an array with the totalStarsnumber of empty slots. Using the spread operator leads to the forced filling of empty slots in undefined, effectively filling the SOMETHING array for display (even if it is undefined).

Just creating an instance of the array with the help Array()does not put spaces in it, it just allocates space for them.

+3
source

spread , , -

function ABC (...) {

console. log(...); //this will print as array [1,3,5]

}

ABC(1,3,5);

, . .

-3

Source: https://habr.com/ru/post/1684788/


All Articles