- ADD a
key
to the root tag inside an iteration (the first tag after the.map
call) - DON't add keys to any other tags
- NEVER use array index as a
key
(it is the default behaviour)
BAD EXAMPLE:
const CatCard = ({ cat }) => (
<div key={cat.id}>
{cat.name}
</div>
);
const CatList = ({ cats }) => (
<div class="CatList">
{cats.map(cat => (
<CatCard cat={cat}> />
))}
</div>
);
GOOD EXAMPLE:
const CatCard = ({ cat }) => (
<div>
{cat.name}
</div>
);
const CatList = ({ cats }) => (
<div class="CatList">
{cats.map(cat => (
<CatCard cat={cat}> key={cat.id} />
))}
</div>
);