React · Lesson 13 of 120
Rendering Lists
Map.
Data usually comes as lists. A list of bikes, a list of orders. To show a list, we turn each item into JSX with the array map method.
- Data comes in lists
- map
- Item to JSX
A list of bikes
Bikes is an array of objects. Map turns each bike into a list item. The resulting array of items goes straight into the list. Run it.
App.jsxjsx
const bikes = [
{ id: 1, model: "Trail 500", price: 849.5 },
{ id: 2, model: "City 200", price: 450 },
{ id: 3, model: "Volt 7", price: 2199 },
];
export default function App() {
return (
<ul>
{bikes.map(bike => (
<li key={bike.id}>{bike.model} — £{bike.price}</li>
))}
</ul>
);
}Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}Summary
Map turns an array of data into an array of JSX. Next, keys.
Key takeaways- bikes.map(...)
- One item each
- Into the list
Next up: Keys — why React needs them