Rendering an array.map() in React

前端 未结 9 1917
渐次进展
渐次进展 2020-12-04 11:17

I am having a problem where I am trying to use array of data to render a

    element. In the code below the console.log\'s are working fine,
9条回答
  •  抹茶落季
    2020-12-04 12:04

    Gosha Arinich is right, you should return your

  • element. But, nevertheless, you should get nasty red warning in the browser console in this case

    Each child in an array or iterator should have a unique "key" prop.

    so, you need to add "key" to your list:

    this.state.data.map(function(item, i){
      console.log('test');
      return 
  • Test
  • })

    or drop the console.log() and do a beautiful oneliner, using es6 arrow functions:

    this.state.data.map((item,i) => 
  • Test
  • )

    IMPORTANT UPDATE:

    The answer above is solving the current problem, but as Sergey mentioned in the comments: using the key depending on the map index is BAD if you want to do some filtering and sorting. In that case use the item.id if id already there, or just generate unique ids for it.

提交回复
热议问题