Lesson 16/100

Tutorials React.js Tutorial

React Lists & Keys — Complete Guide

React Lists & Keys — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.

On this page

React.js Tutorial · Lesson 16 of 100

React Lists & Keys

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Learn by example · ~6 min · Module 2: Props, Events & Lists

What is this?

Render lists by mapping an array to JSX. Each item needs a unique key prop so React can track which row changed, added, or removed.

Why should you care?

Without keys, React may update the wrong DOM nodes when the list reorders — causing bugs and lost input focus.

See it live — copy this example

Paste into src/App.jsx (or the file noted in the steps). Save and watch the browser update.

const todos = [
  { id: 1, text: 'Buy milk' },
  { id: 2, text: 'Call mom' }
];

<ul>
  {todos.map(todo => (
    <li key={todo.id}>{todo.text}</li>
  ))}
</ul>

Run Example »

Edit the code and click Run to see the result update live.

Code
Result

What happened?

  • key={todo.id} should be stable and unique among siblings.
  • Use database ids, not array index, when items can be reordered or deleted.

Try it yourself

  1. Map an array to
  2. or table rows.
  3. Add key from a unique id field.
  4. Filter the array before map if needed.
  5. Change text or labels in the example and save — watch the browser update.
  6. Break the code on purpose (remove a bracket), read the error message, then fix it.

Remember

Use .map() to render lists. Always add a stable unique key. Prefer id over index when list changes.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior PDF Detailed
What is the purpose of keys in React lists?
Short answer: Keys help React identify which items have changed, been added, or removed. ✅ Example code {items.map(item =&gt; ( &lt;li key={item.id}&gt;{item.name}&lt;/li&gt; ))} 🛑 Avoid using array index as key unless…
Mid PDF Detailed
Inefficient List Rendering: Rendering large lists or complex components without?
Short answer: optimization can degrade performance, especially if each item is re-rendered every time the parent changes. Real-world example (ShopNest) When rendering cart lines, use a stable key={item.id} —not the array…
Mid PDF Detailed
React Developer Tools:?
Short answer: Use the Profiler tab to measure the render times and identify slow components. Check for unnecessary re-renders and optimize with React.memo or shouldComponentUpdate. Real-world example (ShopNest) ShopNest’…
Mid PDF Detailed
Mock axios:?
Short answer: import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data' }); Example code import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data'…
Mid PDF Detailed
Choose a Testing Library: Use libraries like Jest (test runner) and React Testing?
Short answer: Library (for testing React components). Real-world example (ShopNest) ShopNest’s storefront is React: components for ProductCard, CartDrawer, and CheckoutForm, with hooks for local UI state. Say this in the…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details