简体   繁体   中英

get index failed using es2015 map

{map(arr, (obj,index) => 
  <div key={index}>{obj.name}</div>
</div>)}

What's wrong with my jsx above? couldn't get the index using map?

Here is the proper way to use map.

array.map((x, index)=>{
     return (<div key={index}>{x.name}</div>);
});

or

Array.prototype.map.call(arr, function(x, index) {
    return (<div key={index}>{x.name}</div>);
});

Mozilla

Array.prototype.map()

You are not wrapping your JSX content within () . Also you need to have the ( on the same line as => and you have an extra closing div

{map(arr, (obj,index) => (
  <div key={index}>{obj.name}</div>
))}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM