5
When rendering a dynamic list in React or React Native, I receive the following warning:
Warning: Each Child in a list should have a Unique "key" prop.
Despite this, things seem to work well without the prop key
. Behold this example:
function shuffleArray(array) {
var currentIndex = array.length,
randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex]
];
}
return array;
}
function App() {
const [pokemons, setPokemons] = React.useState([
{
name: "Bulbasaur",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/001.png"
},
{
name: "Ivysaur",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/002.png"
},
{
name: "Venusaur",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/003.png"
},
{
name: "Charmander",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/004.png"
},
{
name: "Charmeleon",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/005.png"
},
{
name: "Charizard",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/006.png"
},
{
name: "Squirtle",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/007.png"
},
{
name: "Wartortle",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/008.png"
},
{
name: "Blastoise",
img: "https://assets.pokemon.com/assets/cms2/img/pokedex/detail/009.png"
}
]);
function shuffle() {
setPokemons(shuffleArray([...pokemons]));
}
return (
<ul>
<button onClick={shuffle}>Embaralhar</button>
{pokemons.map((pokemon) => (
<li onClick={() => console.log(pokemon.name)}>
<img src={pokemon.img} alt={pokemon.name} />
<span>{pokemon.name}</span>
</li>
))}
</ul>
);
}
ReactDOM.render(<App /> , document.querySelector("#app"));
ul {
padding-right: 2rem;
padding-left: 2rem;
}
li {
list-style: none;
display: flex;
align-items: center;
padding: 10px;
border: 2px solid #ccc;
}
li:hover {
background-color: #deebff;
cursor: pointer;
}
img {
height: 40px;
width: 40px;
}
span {
font-size: 20px;
}
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<div id="app"></div>
To resolve this warning, I have found several answers here using the index as key
, then I have the following doubts:
What is this property for
key
?I need to define a
key
just walking through an array?How can I define a key? Can I use the index, a random number or have another better way?
Before the answers come, I want to make it clear that the goal here is to create a canonical question, not just solve the warning. Thus, this question can be referenced when this problem is noticed in another question of the site (as I have seen several times).
– Rafael Tavares
Are the motives the same as Angular and Vuejs? If so, perhaps it would be better to remove the word React? Since the intention is to create a canonical one.
– Danizavtz
@Danizavtz, in this case I think it would be better to have a canonical one for each library (or framework, hehe), since each one can have its respective peculiarities, so that it can get confusing to do everything in one answer. : -) But of course this is for the AP to decide.
– Luiz Felipe
@Danizavtz what are the reasons to use in angular and vuejs?
– novic
Rafael Tavares the answer given by me lacks something?
– novic
@novic Your answer is good, I even voted for it. If you want to leave it complete, I think you could mention the reason for the
key
impact performance (I explained briefly here), This would also explain why there were unexpected behaviors that you mentioned; and I think there was a lack of clarification when you can use indexes as a key. For example, a list generated from an array that does not change the order, nor inserts items in the beginning/middle and nor removes, has no problems using the index, the reason is explained in the answer that Linkei.– Rafael Tavares
@novic Rereading my answer, I saw that it is important to mention also the danger in the use of unstable keys, as the
Math.random()
ornew Date()
– Rafael Tavares
This answers your question? REACT-JS - Warning: Each Child in a list should have a Unique "key" prop
– Leo Brescia
@Leobrescia is related but does not answer. It answers (more or less) only to the second point (I need to define a key only when traversing an array?), and only answers this because of the answer, the question is misspelled and just wants to solve the error.
– Rafael Tavares