Understanding unique keys for array children in React.js

后端 未结 12 1302
时光取名叫无心
时光取名叫无心 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:22

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

    This is a warning as for array items which we are going to iterate over will need a unique resemblance.

    React handles iterating component rendering as arrays.

    Better way to resolve this is provide index on the array items you are going to iterate over.for example:

    class UsersState extends Component
        {
            state = {
                users: [
                    {name:"shashank", age:20},
                    {name:"vardan", age:30},
                    {name:"somya", age:40}
                ]
            }
        render()
            {
                return(
                        
    { this.state.users.map((user, index)=>{ return {user.name} }) }
    ) }

    index is React built-in props.

提交回复
热议问题