Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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>

                                    </>
                                )
                            })
                        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
        )
    })
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading