Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Why am I getting 'Each child in a list should have a unique "key" prop error' When I have a key assigned?

I have a header navigation component that renders navigation from two parts of a JSON response.

The navigation renders correctly but I am getting the: 'Each child in a list should have a unique "key" prop error' even though from what I can see I do have a key for each child element.

The Id from both map calls will be completely unique.

This is the complete code for the component so far:

import React, { useContext } from 'react';
import { Link } from "react-router-dom";
import { HeaderContext } from '../context/headerContext';

const renderToplevelPages = (toplevelPages) => {
    return toplevelPages.map(({ PageUrl, NavLinkText, Id, SubNavMenuItems }) => (
        <li key={Id}><Link to={PageUrl}>{NavLinkText}</Link>
            {SubNavMenuItems && <ul>{renderToplevelPages(SubNavMenuItems)}</ul>}
        </li>
    ));
};

function Header() {

    const headerData = useContext(HeaderContext);

    return (
        <nav>
            <Link to="/">Logo here</Link>
            {headerData
                ? <ul>
                    <li>
                        <Link to={headerData.Menu.PageUrl}>Menu</Link>
                        <ul>
                            {headerData.Menu.Categories.map(categoryItem => (
                                <li key={categoryItem.Id}><Link to={categoryItem.CategoryUrl}>{categoryItem.CategoryName}</Link></li>
                            ))}
                        </ul>
                    </li>
                    <li>
                        <a className="navbar-icon fa-user snipcart-customer-signin">Login/Register</a>
                    </li>
                    {renderToplevelPages(headerData.TopLevelPages)}
                </ul>
                : <div></div>
            }
        </nav >
    )
}

export default Header;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're missing a key here:

 <li>
     <Link to={headerData.Menu.PageUrl}>Menu</Link>
        ....                
 </li>

and here:

<li>
    <a className="navbar-icon fa-user snipcart-customer-signin">Login/Register</a>
</li>

Just because they're not part of a call to Array.map, they're still children of the parent <ul> so they need a key.

If that doesn't fix it, perhaps your Ids aren't correct - I suggest you check them by rendering the Id instead of e.g. CategoryName.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda