Complete the code to add a unique key to each list item in React.
const items = ['apple', 'banana', 'cherry']; const list = items.map((item, index) => <li key=[1]>{item}</li>);
Using index as the key ensures each list item has a unique identifier in this simple example.
Complete the code to correctly assign keys when rendering a list of objects.
const users = [{id: 1, name: 'Anna'}, {id: 2, name: 'Ben'}];
const list = users.map(user => <div key=[1]>{user.name}</div>);Using user.id as key is best because it is unique and stable.
Fix the error in the code by choosing the correct key value.
const tasks = ['clean', 'cook', 'shop']; return tasks.map(task => <p key=[1]>{task}</p>);
Using task as key works here because task names are unique strings.
Fill both blanks to create a list with unique keys and display user names.
const users = [{id: 10, name: 'Lara'}, {id: 20, name: 'Mike'}];
const list = users.map(user => <li key=[1]>{user.[2]</li>);Use user.id for the key and user.name to display the name.
Fill all three blanks to create a React list with keys and display user emails.
const users = [{uid: 'a1', email: 'a@example.com'}, {uid: 'b2', email: 'b@example.com'}];
const list = users.map(user => <div key=[1]>{user.[2]</div>);
return <section>[3]</section>;Use user.uid as key, access user.email to display emails, and render the list inside the section.
