Loading...
Loading...
Guides proper usage of the key prop in React lists. Use this skill when rendering lists, mapping arrays to components, or troubleshooting list-related state bugs.
npx skill4agent add flpbalada/my-opencode-config react-key-propkey// ✅ Correct
{todos.map((todo) => (
<li key={todo.id}>{todo.text}</li>
))}import { nanoid } from 'nanoid';
useEffect(() => {
fetch('/api/items')
.then(res => res.json())
.then(data => {
const itemsWithIds = data.map(item => ({
...item,
_tempId: nanoid() // Stable ID generated once
}));
setItems(itemsWithIds);
});
}, []);// ❌ WRONG: Creates new key every render
{items.map(item => (
<li key={Math.random()}>{item.name}</li>
))}// ❌ WRONG for dynamic lists
{items.map((item, index) => (
<li key={index}>{item.name}</li>
))}useId()useId()keyidnanoiduuidkeyMath.random()Date.now()indexkeyuseId()