当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - React - Function Parameter Inversion (Index Confusion)

问题描述:

While playing a little bit with React.js, I found it interesting that if you use a function which has the index parameter but as the second instead of the first one, the returned element gets confused.

View Example

http://codepen.io/AlexandruDoda/pen/VPKeOa

As you can see in the Pen, the output shows {element} as if it was the index element. Just a curiosity, anybody aware why this happens behind the scenes?

Cheers!

网友答案:

The unexpected behoaviour is caused by the logic, you're not doing it right. Facebook documentation provides good and bad practises. Let's say you have an arary const lists = ['first', 'second', 'third']. To append it correcty you would shoud do soemthing like this:

{ lists.map(element, index) => <li key={index}>{element}</key> }

If you wanna dig deaper, the documentation provides a lot more informations ;)

分享给朋友:
您可能感兴趣的文章:
随机阅读: