Course contentsShow
← React
Lesson 1247 of 2,39632. Lists and Keys Deep DiveFree lesson

Stable Keys from Server Data: Using IDs

Database IDs or UUIDs from your backend provide perfect stable keys that survive re-fetches and reordering.

Stable Keys from Server Data: Using IDs

You'll learn why database IDs and UUIDs make perfect React keys that remain stable through re-fetches and data changes.

The Gold Standard for Keys

When you fetch data from a backend, each record typically comes with a unique identifier—a database ID, UUID, or similar. These identifiers are perfect for React keys because they never change for a given entity, even when you:

  • Re-fetch the data from the server
  • Sort or filter the list
  • Update other properties of the item
  • Navigate away and back to the page

Think of it like your Social Security Number or passport number—it's permanently tied to you, regardless of where you move, what you change about yourself, or how many times someone looks you up in a database.

Why Server IDs Are Ideal

const users = [
  { id: 'uuid-abc-123', name: 'Alice', age: 30 },
  { id: 'uuid-def-456', name: 'Bob', age: 25 },
  { id: 'uuid-ghi-789', name: 'Charlie', age: 35 }
];

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

Even if you later sort by age or update Alice's name, 'uuid-abc-123' remains stable. React recognizes the same component instance and preserves its internal state (if any) and DOM position correctly.

Contrast with Bad Keys

Using array index would break if you sort by age—Charlie's component might suddenly get Bob's key. Using a generated value like Math.random() creates a new key every render, destroying and recreating components unnecessarily.

Key Takeaway: Always use database IDs or UUIDs from your backend as keys—they provide the stability React needs to track component identity across renders, re-fetches, and data transformations.