Complete the code to render a list of items in React.
const List = () => {
const items = ['apple', 'banana', 'cherry'];
return (
<ul>
{items.map((item, index) => <li key={index}>{item}</li>)}
</ul>
);
};Using index as the key is acceptable for static lists without reordering.
Complete the code to avoid missing keys warning in React list rendering.
const List = () => {
const items = [{id: 1, name: 'apple'}, {id: 2, name: 'banana'}];
return (
<ul>
{items.map(item => <li key={item.id}>{item.name}</li>)}
</ul>
);
};Using a unique stable id like item.id as key prevents React warnings and improves performance.
Fix the error in the code that causes React to warn about keys.
const List = () => {
const items = ['apple', 'banana', 'cherry'];
return (
<ul>
{items.map((item, index) => <li key={index}>{item}</li>)}
</ul>
);
};
// Add [1] to fix the warning.React requires a unique key prop on list items to track them efficiently.
Fill both blanks to correctly render a list with unique keys and avoid common mistakes.
const List = () => {
const items = [{id: 10, name: 'apple'}, {id: 20, name: 'banana'}];
return (
<ul>
{items.map(({id, name}) => <li key={id}>{name}</li>)}
</ul>
);
};Destructure id from each item and use it as the key prop for stable unique keys.
Fill all three blanks to correctly render a list with keys and avoid common React list mistakes.
const List = () => {
const items = [{id: 1, label: 'A'}, {id: 2, label: 'B'}, {id: 3, label: 'C'}];
return (
<ul>
{items.map(({id, label}) => <li key={id}>{label}</li>)}
</ul>
);
};Destructure id and label, use id as key and display label inside the list item.