React Lists & Keys

Render arrays with map().

✍️ 3 exercises + code challenge in this lesson

Render a list by mapping an array to JSX elements: {items.map(item => <li>{item.name}</li>)}

Each item needs a unique key prop so React can track which items changed. Use a stable id from your data, not the array index, when items can be reordered or removed.

Key Takeaways: React Lists & Keys

Mastering React Lists & Keys helps you write clean and reliable React code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

React Lists & Keys Code Example

React
<div id="root"></div>
<script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"></script>

<script type="text/babel">
const { useState, useEffect, useRef, useMemo, createContext, useContext } = React;
const students = [
  { id: 1, name: "Ali", marks: 81 },
  { id: 2, name: "Sara", marks: 94 },
  { id: 3, name: "Usman", marks: 62 },
];

function App() {
  const sorted = [...students].sort((a, b) => b.marks - a.marks);

  return (
    <ol>
      {sorted.map((s) => (
        <li key={s.id}>
          {s.name}: <strong>{s.marks}</strong> {s.marks >= 90 && "🏆"}
        </li>
      ))}
    </ol>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
Free sign-in required · Google login available

React Lists & Keys Practice Exercises

Check what you learned in “React Lists & Keys”.

/ 3 solved
Fill in the blank

Every list item needs a unique…

{items.map(item => <li ={item.id}>{item.name}</li>)}
✓ Correct! Not quite. Try again.
Multiple choice

Which array method is used to render lists in JSX?

✓ Correct! Not quite. Try again.
Code challenge

Render the fruits array as <li> items inside a <ul>.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services