Understanding unique keys for array children in React.js

后端 未结 12 1304
时光取名叫无心
时光取名叫无心 2020-11-21 05:45

I\'m building a React component that accepts a JSON data source and creates a sortable table.
Each of the dynamic data rows has a unique key assigned to it but I\'m stil

12条回答
  •  日久生厌
    2020-11-21 06:25

    This may or not help someone, but it might be a quick reference. This is also similar to all the answers presented above.

    I have a lot of locations that generate list using the structure below:

    return (
        {myList.map(item => (
           <>
              
    {item.someProperty} ....
    )} )

    After a little trial and error (and some frustrations), adding a key property to the outermost block resolved it. Also, note that the <> tag is now replaced with the

    tag now.

    return (
      
        {myList.map((item, index) => (
           
    {item.someProperty} ....
    )} )

    Of course, I've been naively using the iterating index (index) to populate the key value in the above example. Ideally, you'd use something which is unique to the list item.

提交回复
热议问题