Home > Software design >  Each child in a list should have a unique "key" prop Reactjs
Each child in a list should have a unique "key" prop Reactjs

Time:01-05

I am working on Nextjs/Reactjs i am fetching data after click on button but in my console i am getting following error

Each child in a list should have a unique "key" prop

Here is my current code ( data is displaying with id and title)(

<button onClick={fetchReviewsFromServer}>Load Reviews From Server</button>
                        {
                            reviewfromserver.map(reviewnew=>{
                                return(
                                    <>
                                       <div key={reviewnew.id}> // not working
                                            <div>{reviewnew.id}- {reviewnew.title} </div>
                                       </div>

                                    </>
                                )
                            })
                        }

CodePudding user response:

The child in your list is the empty <> element, I don't see a need for it regardless, since you only have one child element to that element.

<button onClick={fetchReviewsFromServer}>Load Reviews From Server</button>
{
    reviewfromserver.map(reviewnew=>{
        return(
            <div key={reviewnew.id}> // not working
                <div>{reviewnew.id}- {reviewnew.title} </div>
            </div>
        )
    })
}

CodePudding user response:

That's because the fragmets are items. Remove them, you don't need them anyway.

  • Related