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

573
Visualizações
Each child in a list should have a unique "key" prop. I made it, but still see this error

I know about key prop, so i made it in listitem component

const ListItem = ({item}) => {
const {result, time, id} = item;
return(
    <li key={id} className='list__item'>
        <span className='item__result'>{result} cps</span>
        <span className='item__date'>{time}</span>
        <button className='item__delete'>delete</button>
    </li>
)}

And here is component, where I use it

const Leadboard = () => {
const [data, setData] = useState([{result:'5,63', time:'08.06.2022', id:  (new Date()).toString(16)}, {result:'5,63', time:'08.06.2022', id: +(new Date() - 1)}, {result:'5,63', time:'08.06.2022', id: +(new Date() - 2)}]);

let elements=data.map(item => {
    return (
        <>
            <ListItem item={item} />
        </>
    )
});

return(
    <div className='app-leadboard'>
        <span className='app-leadboard__title'>Your's best results:</span>
        <ol className='app-leadboard__list' type='1'>
            {elements}
        </ol>
    </div>
)}

But after render I still see "key prop" error

I spent so much time on this, but can't understand what's wrong. So smb, help pls with it

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You’ve got the wrong list. It’s the <ListItem> components that need the key. (And you can get rid of the react fragments around them because they are pointless).

about 4 years ago · Juan Pablo Isaza Relatório

0

React first accesses the empty bracket (<> </> ) before accessing the key attribute in your child component.

So you need to either

  1. Make use of the empty brackets and pass the key attribute to it
// Use React.Fragment 

  let elements=data.map(item => { return 
  (
   <React.Fragment key={item.id}>
 <ListItem item={item} />
</React.Fragment>
)
});

and remove the key in the child (ListItem) component

ListItem.js
 <li 
 /*  Remove this
key={id} 
*/

  className='list__item'>

OR

Get rid of the empty brackets (<> </>) and reference the child's component directly.

let elements=data.map(item => {
return (<ListItem item={item} />)
});

and leave the key attribute in the child component

More on React Fragment. React Official Docs

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