Loading Depth Study
Preparing your workspace
React can render lists by transforming arrays into JSX elements, and each sibling in a list needs a stable key.
Most applications display collections such as courses, lessons, students, or notifications. The map method is commonly used to turn array data into React elements. The key prop helps React identify which list item corresponds to which data item across renders. Stable unique identifiers from the data are usually the best keys. Array indexes can produce incorrect behavior when items are inserted, removed, or reordered, so they should only be used in suitable static cases.
A course page can render all lessons from an array and use each lesson's database ID as its key.
function LessonList({ lessons }) {
return (
<ul>
{lessons.map((lesson) => (
<li key={lesson.id}>
{lesson.title}
</li>
))}
</ul>
);
}The component renders each lesson as a list item using its unique ID as the key.