Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

286
Visualizações
how to add key to chain mapping in react js, butI dont have anything specific to put as unique key

I am mapping arrays without adding keys to the children which cause this error.

react_devtools_backend.js:4026 Warning: Each child in a list should have a unique "key" prop.

how can I get rid of this error with these structure only without anything specific to each child in each mapping!

Here is my array structure:

[
  [
    { title: 'McRoyale', price: 70, count:5, totalPrice: 350 },
    { title: 'Big Mac', price: 55, count:1, totalPrice: 55 },
  ],
  [
    { title: 'Double Big Tasty', price: 99, count:2, totalPrice: 198 },
  ],
  [
    { title: 'Grand Chicken Premier', price: 72, count:3, totalPrice: 216 },
    { title: 'Spicy Chicken Fillet', price: 60, count:2, totalPrice: 120 },
  ]
]

and here is my code:

<tbody>
          {cardItems.map((items) => (
            <tr>
              <td>
                {items.map((item) => (
                  <>
                    <b className='mc-red'>{item.title}</b> Item Price:{' '}
                    {item.price} LE, Item Count: {item.count}, Item Total Price:{' '}
                    {item.totalPrice} LE
                    <br />
                  </>
                ))}
              </td>
              <td>{calcPrice(items)} LE</td>
            </tr>
          ))}
        </tbody>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

JSX elements that are dynamically created (e.g. using map) must have a key prop, according to the official doc.

Another note is that, this key must be consistent even if the dynamic logic changes. For example, you can't use the element's index in the array as a key because that can change. You must use some value that is consistent.

With that in mind, it is simple to use each item's title as the key for you inner map. For the outer map though, if you can ensure items never changes:

// This never changes
[
  { title: 'McRoyale', price: 70, count:5, totalPrice: 350 },
  { title: 'Big Mac', price: 55, count:1, totalPrice: 55 },
]

then you can use the all its items' titles to make the key, like this:

import { Fragment } from 'react';

<tbody>
  {cardItems.map((items) => (
    <tr key={items.map((item) => item.title).join('')}>
      <td>
        {items.map((item) => (
          <Fragment key={item.title}>
            <b className="mc-red">{item.title}</b> Item Price: {item.price}{' '}
            LE, Item Count: {item.count}, Item Total Price:{' '}
            {item.totalPrice} LE
            <br />
          </Fragment>
        ))}
      </td>
      <td>{calcPrice(items)} LE</td>
    </tr>
  ))}
</tbody>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda