Home > Software engineering >  what is the difference between mapping items inside and outside component main return?
what is the difference between mapping items inside and outside component main return?

Time:11-26

What is the difference between mapping inside the return statement or outside of it then adding the mapped array to the component return statement? Is there a performance gain or just for organizing?

const Component ()=>  {
   const itmes = arr.map((el)=> {
      return <div>el.item</div>
   })
   return 
   <div>
      {items}
   </div>
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Just for organizing. The code being executed is the exact same.

There is (an extremely) marginal gain in doing it directly in the return statement: you save on variable (items) declaration, but please do not worry about that, your code won't benefit from this.

  • Related