Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
React map function not assinging keys properly

for some reason, I get the "each child in a list should have unique key" error when returning the following code. I do not understand why this happens, as I specifically assign the key during mapping:

return (
        <>
            {sortfeedCards(feedCards)}
            {loggedIn === true ? (feedCardsMod.map((card, index) => (
                <>
                    <p>{index}</p>
                    <FeedCard key={index} cardData={card} loggedIn={loggedIn} />
                </>
            ))) : ('')}
        </>
    )

And here's what the render looks like.. it seems to me that the index-variable does work: A picture of the feed showing correct index numbers

Many Thanks in advance!

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

The key needs to be on the outermost element, so on the Fragment, not the FeedCard:

feedCardsMod.map((card, index) => (
  <React.Fragment key={index}>
    <p>{index}</p>
    <FeedCard cardData={card} loggedIn={loggedIn} />
  <React.Fragment/>
))

(The shorthand syntax <></> for fragments doesn't allow keys, so i switched to using React.Fragment explicitly)

about 4 years ago · Santiago Gelvez Report

0

Try this:

return (
    <>
        {sortfeedCards(feedCards)}
        {loggedIn === true ? (feedCardsMod.map((card, index) => (
            <div key={index}>
                <p>{index}</p>
                <FeedCard cardData={card} loggedIn={loggedIn} />
            </div>
        ))) : ('')}
    </>
)

Keys must be unique amongst the enclosing tags: https://reactjs.org/docs/lists-and-keys.html

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!