.map()Concept: Refactoring list rendering with array methods
We’ve already built FruitList with a manual for loop back in Kata 5. Now let’s revisit that kata and refactor it using the built-in .map() array method.
Create a FruitListMap component that:
['Apple', 'Banana', 'Orange', 'Grape']<ul>) with <li> items generated with .map()• Apple
• Banana
• Orange
• Grape
export default function FruitListMap() {
const fruits = ['Apple', 'Banana', 'Orange', 'Grape'];
return (
<ul>
{/* Use .map() here */}
</ul>
);
}
.map() transforms each item in an array and returns a new array.map(), return <li key={fruit}>...</li>.map() call right inside the JSX { ... }.map() is perfect for transforming arrays into JSX.map() over data to transform objects into JSX because array helpers run cleanly inside {} expressions