Ojasa Mirai

Ojasa Mirai

ReactJS

Loading...

Learning Level

🟢 Beginner🔵 Advanced
📋 Rendering Lists with map()🔑 Keys and Reconciliation✨ Unique Keys⚠️ Common Key Mistakes🔍 Filtering Lists↕️ Sorting and Reordering➕ Dynamic Lists
Reactjs/Lists Keys/Unique Keys

✨ Unique Keys

Creating unique keys is essential for React list rendering.

Sources of Unique Keys

Database IDs (Preferred)

function UserList({ users }) {
  return (
    <ul>
      {users.map(user => (
        <li key={user.id}>{user.name}</li>
      ))}
    </ul>
  );
}

UUID Generation

import { v4 as uuidv4 } from "uuid";

function NoteList() {
  const [notes, setNotes] = React.useState([]);

  const addNote = () => {
    setNotes([...notes, { id: uuidv4(), text: "" }]);
  };

  return (
    <div>
      {notes.map(note => (
        <input key={note.id} defaultValue={note.text} />
      ))}
      <button onClick={addNote}>Add Note</button>
    </div>
  );
}

Combining Multiple Properties

function CommentList({ userId, comments }) {
  return (
    <ul>
      {comments.map(comment => (
        <li key={\`{userId}-{comment.timestamp}\`}>
          {comment.text}
        </li>
      ))}
    </ul>
  );
}

When NOT to Use Index

// ❌ Bad: Index changes when items reorder
{items.map((item, i) => <li key={i}>{item.text}</li>)}

// ✅ Good: Stable ID
{items.map(item => <li key={item.id}>{item.text}</li>)}

Unique vs Stable Keys

PropertyImportance
UniqueNo duplicates in list
StableSame data = same key across renders
ConsistentDoesn't change between renders

Testing Key Uniqueness

function validateKeys(items) {
  const keys = items.map(item => item.id);
  return new Set(keys).size === keys.length; // True if unique
}

const users = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" }
];
console.log(validateKeys(users)); // true

✅ Key Takeaways

  • Use **database IDs** when available
  • Generate **UUIDs** for client-side data
  • Keys must be **unique** within the list
  • Keys must be **stable** between renders
  • Never use **index as key** (except static lists)

Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn