Option A returns JSX elements with unique keys, which is the correct pattern for rendering lists.
What will happen if you use the index of the array as the key in map()?
AIt styles the list items
BIt always works perfectly
CIt prevents React from rendering the list
DIt can cause problems if the list changes order or items are added/removed
✗ Incorrect
Using index as key can cause UI bugs when list items change order or are added/removed.
Can map() be used to change the content of list items before rendering?
ANo, it only copies the original items
BYes, it can transform data before returning JSX
CNo, it only filters items
DYes, but only for numbers
✗ Incorrect
map() can transform or format data before rendering JSX elements.
Explain how to use the map() function in React to render a list of items with unique keys.
Think about how you turn an array into visible elements on the screen.
You got /4 concepts.
Describe why the key prop is important when rendering lists with map() in React.
Consider how React knows which items changed when the list updates.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using the map function in React when rendering lists?
easy
A. To mutate the original array directly
B. To remove duplicate elements from the array
C. To sort the array elements alphabetically
D. To transform an array into a list of React elements
Solution
Step 1: Understand what map does in JavaScript
The map function creates a new array by applying a function to each element of the original array without changing it.
Step 2: Apply this to React rendering
In React, map is used to convert each item in an array into a React element, which can then be rendered as a list.
Final Answer:
To transform an array into a list of React elements -> Option D
Quick Check:
map creates React elements from arrays = B [OK]
Hint: Remember: map creates new arrays, perfect for rendering lists [OK]
Common Mistakes:
Thinking map changes the original array
Confusing map with sorting or filtering
Not realizing map returns a new array
2. Which of the following is the correct syntax to use map inside a React component to render a list of names stored in const names = ['Anna', 'Bob', 'Cara']?
easy
A. {names.map(name => <li key={name}>{name}</li>)}
B. {names.map(name => <li>name</li>)}
C. {names.forEach(name => <li key={name}>{name}</li>)}
D. {names.map(name => <li>{name}</li> key={name})}
Solution
Step 1: Check the correct use of map and JSX
Using map with an arrow function returning JSX elements is correct. The element must have a unique key prop inside the returned JSX.
Step 2: Identify the correct syntax
{names.map(name => <li key={name}>{name}</li>)} correctly uses map, returns a list item with curly braces around name, and includes a unique key prop.
Final Answer:
{names.map(name => <li key={name}>{name}</li>)} -> Option A
Quick Check:
map + JSX + key prop = A [OK]
Hint: Always add a unique key prop inside map JSX elements [OK]
Common Mistakes:
Using forEach instead of map
Missing curly braces around variables in JSX
Placing key prop outside the JSX element
3. What will be the output of this React code snippet?
elements but none have a key prop, which React requires for lists.
Step 2: Confirm other syntax is correct
Curly braces around item are correct, map is valid inside JSX, and array can contain strings.
Final Answer:
Missing unique key prop on <li> elements -> Option B
Quick Check:
React needs keys in lists = C [OK]
Hint: Always add key prop to elements inside map [OK]
Common Mistakes:
Ignoring missing key warnings
Thinking keys are optional
Confusing key prop placement
5. Given this nested array of user objects:
const users = [
{ id: 1, name: 'Alice', hobbies: ['Reading', 'Hiking'] },
{ id: 2, name: 'Bob', hobbies: ['Cooking'] }
];
// Which code correctly renders a list of users with their hobbies?
hard
A. {users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={user.id}>{hobby}</li>)}
B. {users.map(user => <div><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li>{hobby}</li>)}
C. {users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={hobby}>{hobby}</li>)}
D. {users.map(user => <div key={user.name}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={user.id}>{hobby}</li>)}
Solution
Step 1: Check keys on outer and inner lists
The outer div uses user.id as key, which is unique. The inner li uses hobby as key, which is unique per hobby.
Step 2: Verify correct JSX structure and keys
{users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={hobby}>{hobby}</li>)} correctly nests the hobbies list inside each user and assigns proper keys. Other options miss keys or reuse keys incorrectly.
Final Answer:
{users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={hobby}>{hobby}</li>)} -> Option C
Quick Check:
Unique keys on nested lists = A [OK]
Hint: Use unique keys for both outer and inner mapped elements [OK]
Common Mistakes:
Reusing same key for multiple elements
Missing key on inner map elements
Using non-unique keys like user name or id incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.