Hackforge Academy

Category: react

List and keys in react with coding and examples

Published on 14 Feb 2026

Explanation

What Are Lists in React?
Lists: 1.render multiple elements 2.Created using .map()
Keys: 1.Unique identifier 2.Help React track changes 3.Should be stable & unique 4.Avoid index if list changes

Code:


Explanation

Basic Example: Rendering a List

Code:

function App() {
  const fruits = ["Apple", "Banana", "Mango"];

  return (
    <div>
      <h2>Fruit List</h2>
      <ul>
        {fruits.map((fruit) => (
          <li>{fruit}</li>
        ))}
      </ul>
    </div>
  );
}

export default App;

Explanation

What Are Keys in React? A key is a special prop:
Identify which items 1. changed 2.added 3.removed Optimize re-rendering

Code:

function App() {
  const fruits = ["Apple", "Banana", "Mango"];

  return (
    <ul>
      {fruits.map((fruit, index) => (
        <li key={index}>{fruit}</li>
      ))}
    </ul>
  );
}

Explanation

Using Index as Key Is Not Always Good Using index works only when: List does NOT change order Items are NOT deleted/inserted If list changes dynamically use unique ID.

Code:

const students [{"name":"asfd"}];
  return (
    <ul>
      {students.map((student) => (
        <li key={student.id}>
          {student.name}
      </li>
      ))}
    </ul>
  );

Explanation


Lists: 1.render multiple elements 2.created using .map()
Keys: 1.Unique identifier 2.Help React track changes 3.Should be stable & unique 4.Avoid index if list changes

Code:


Explanation

Why do we use a key prop in React lists?

Code:

A) To apply CSS styles
B) To uniquely identify list items
C) To create new components
D) To store state

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community