Concept: List pattern - displaying arrays in JSX
Create a FruitList component that:
['Apple', 'Banana', 'Orange', 'Grape']<ul>) with list items (<li>)• Apple
• Banana
• Orange
• Grape
export default function FruitList() {
const fruits = ['Apple', 'Banana', 'Orange', 'Grape'];
// Build array of list items using a for loop
return (
<ul>
{/* Display the array of list items here */}
</ul>
);
}
const listItems = [];<li> elements into the arraykey prop to each <li>: <li key={i}>{fruits[i]}</li>{listItems}export default function FruitList() {
const fruits = ['Apple', 'Banana', 'Orange', 'Grape'];
// Build list items using a for loop
const listItems = [];
for (let i = 0; i < fruits.length; i++) {
listItems.push(<li key={i}>{fruits[i]}</li>);
}
return (
<ul>
{listItems}
</ul>
);
}
**Note:** You can also use `.map()` as a shortcut:
{fruits.map((fruit, index) => <li key={index}>{fruit}</li>)}
key prop for React to track changes.map() is a built-in method that does this automaticallyNote: Learn more about the built-in .map() method at MDN: Array.map()