P
P
PurgaBot2021-10-29 22:05:14
React
PurgaBot, 2021-10-29 22:05:14

Why is {} put with a function argument?

const StarRating = function({countStar}) {
  return(createArray(countStar).map((n,i) => <Star key={i}/>))
}

Why is {} between the function argument in React in this code? Everything works with them ({}) and the .map() function runs everything as it should, but without it only depicts 1 object, but 5 is needed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Frontend developer, 2021-10-29
@markak

When creating a component, the props object is passed to the function, all jsx attributes of the component are collected in the props object.
Therefore, without parentheses, it will be like this:

const StarRating = function(props) {
  return(createArray(props.countStar).map((n,i) => <Star key={i}/>))
}

T
Test-style, 2021-10-31
@Test-style

In other words: an argument arrives in the function, which has the countStar property. In order to immediately operate with what lies in this property, {} is done - destructuring.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question