React warning: Each child in a list should have a unique "key" prop
poze 5 months ago644 gadeen
5
{todos.map((t, i) => <TodoItem todo={t} />)}
I get the key warning. If I use key={i} it goes away. Is the index OK as a key?
Dyagnostik IA
Se IA ki fè l. Se pa yon repons — kominote a anba a konfime oswa korije l. Toujou verifye anvan ou fye l.
poze 5 months ago
Marie Louis 2.2k
1 Repons
5
✓✓ Repons yo aksepte
React uses key to match list items between renders. Use a stable, unique id from your data:
{todos.map(t => <TodoItem key={t.id} todo={t} />)}
The index is only safe if the list is never reordered, filtered, or inserted into. Otherwise React reuses the wrong component instance, and you get bugs like an input keeping the text of the item that was deleted.
If your items have no id, generate one when you *create* the item (crypto.randomUUID()), not during render.
Konekte pou di lòt moun si li te mache.
answered 5 months ago
Jean Pierre 5.1k
Sign in and verify your email to post an answer.