Consider this React component that uses map to render a list of items:
function FruitList() {
const fruits = ['Apple', 'Banana', 'Cherry'];
return (
<ul>
{fruits.map((fruit, index) => <li key={index}>{fruit}</li>)}
</ul>
);
}What will this component render in the browser?
function FruitList() { const fruits = ['Apple', 'Banana', 'Cherry']; return ( <ul> {fruits.map((fruit, index) => <li key={index}>{fruit}</li>)} </ul> ); }
Remember that map returns a new array of elements, and React renders each element inside the list.
The map function creates a new array of <li> elements, each containing the fruit name. The key prop helps React identify each item uniquely.
map to double numbers in React JSX?You want to display a list of doubled numbers from an array [1, 2, 3]. Which code snippet correctly uses map inside JSX?
Check for correct arrow function syntax and proper use of key attribute.
Option A correctly uses an arrow function and includes a unique key prop. Option A has an invalid key attribute (missing value), Option A has a syntax error, and Option A renders the string 'num * 2' literally.
Look at this component:
function Colors() {
const colors = ['red', 'green', 'blue'];
return (
<div>
{colors.map(color => <span>{color}</span>)}
</div>
);
}What is the cause of the warning in the browser console?
function Colors() { const colors = ['red', 'green', 'blue']; return ( <div> {colors.map(color => <span>{color}</span>)} </div> ); }
React requires a special prop when rendering lists to track items.
React needs a unique key prop on each element created in a list to optimize rendering. Without it, React shows a warning.
Consider this component that uses map and state:
import { useState } from 'react';
function CounterList() {
const [counts, setCounts] = useState([0, 0, 0]);
function incrementFirst() {
const newCounts = counts.map((count, index) => index === 0 ? count + 1 : count);
setCounts(newCounts);
}
return (
<div>
{counts.map((count, i) => <p key={i}>Count {i}: {count}</p>)}
<button onClick={incrementFirst}>Increment First</button>
</div>
);
}What will be displayed after clicking the button once?
import { useState } from 'react'; function CounterList() { const [counts, setCounts] = useState([0, 0, 0]); function incrementFirst() { const newCounts = counts.map((count, index) => index === 0 ? count + 1 : count); setCounts(newCounts); } return ( <div> {counts.map((count, i) => <p key={i}>Count {i}: {count}</p>)} <button onClick={incrementFirst}>Increment First</button> </div> ); }
Check which index is incremented in the map function.
The map function adds 1 only to the first element (index 0). The others remain unchanged.
map sometimes problematic?In React, when rendering lists with map, developers often use the array index as the key prop. Why can this cause issues?
Think about what happens when the list changes dynamically.
When list items reorder or change, using index as key can cause React to reuse components incorrectly, leading to bugs or wrong UI updates.