Complete the code to render a list of names using the map method.
const names = ['Anna', 'Ben', 'Cara']; function NameList() { return ( <ul> {names.[1](name => <li key={name}>{name}</li>)} </ul> ); }
The map method is used to transform each item in the array into a React element for rendering.
Complete the code to add a unique key prop to each list item.
const items = ['apple', 'banana', 'cherry']; function FruitList() { return ( <ul> {items.map(item => <li [1]={item}>{item}</li>)} </ul> ); }
id or name instead of key.React requires a key prop on list items to help identify which items have changed, added, or removed.
Fix the error in the code by completing the blank to correctly render list items with keys.
const tasks = ['clean', 'cook', 'shop']; function TaskList() { return ( <ul> {tasks.map((task, [1]) => <li key={index}>{task}</li>)} </ul> ); }
key as a parameter name, which is not correct.The second parameter of the map callback is the index, which can be used as a key if no unique id is available.
Fill both blanks to render a list of user objects showing their names with unique keys.
const users = [{id: 1, name: 'Sam'}, {id: 2, name: 'Liz'}];
function UserList() {
return (
<ul>
{users.map(user => <li [1]={user.[2]>{user.name}</li>)}
</ul>
);
}name or index as keys which may not be unique.The key prop should be set to a unique value like user.id to help React identify list items.
Fill all three blanks to render a list of products with keys and display their prices.
const products = [{code: 'A1', price: 10}, {code: 'B2', price: 20}];
function ProductList() {
return (
<ul>
{products.map(product => (
<li [1]={product.[2]>
{product.[3] USD
</li>
))}
</ul>
);
}Use key with a unique product code, and display the price property inside the list item.