Performance: Map function usage
This affects rendering speed and responsiveness by controlling how many DOM elements React creates and updates.
const items = ['a', 'b', 'c']; return <div>{items.map(item => <div key={item}>{item}</div>)}</div>;
const items = ['a', 'b', 'c']; return <div>{items.map(item => <div>{item}</div>)}</div>;
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Map without keys | High (all items updated) | Many reflows | High paint cost | [X] Bad |
| Map with unique keys | Minimal (only changed items) | Few reflows | Lower paint cost | [OK] Good |
| Map large list without virtualization | Very high (all 1000+ items) | Many reflows | Very high paint cost | [X] Bad |
| Map large list with virtualization | Low (only visible items) | Few reflows | Low paint cost | [OK] Good |
map function in React when rendering lists?map does in JavaScriptmap function creates a new array by applying a function to each element of the original array without changing it.map is used to convert each item in an array into a React element, which can then be rendered as a list.map inside a React component to render a list of names stored in const names = ['Anna', 'Bob', 'Cara']?map and JSXmap with an arrow function returning JSX elements is correct. The element must have a unique key prop inside the returned JSX.{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.const numbers = [1, 2, 3];
const listItems = numbers.map(n => <li key={n}>Number: {n * 2}</li>);
return <ul>{listItems}</ul>;map:const items = ['a', 'b', 'c'];
return <ul>{items.map(item => <li>{item}</li>)};map function returns key prop, which React requires for lists.item are correct, map is valid inside JSX, and array can contain strings.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?div uses user.id as key, which is unique. The inner li uses hobby as key, which is unique per hobby.{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.Pass the test and this pattern moves unknown