πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Keys and Lists

React Fundamentals🟒 Free Lesson

Advertisement

Keys and Lists

Rendering lists, key prop, stable keys, and reconciliation patterns.

Overview

Keys help React identify which items have changed, been added, or removed in lists.

Key Concepts

  • key Prop β€” Unique identifier for list items
  • Stable Keys β€” Use IDs, not array indices
  • Reconciliation β€” React's algorithm for updating the DOM
  • Fragment Keys β€” Keys needed when rendering lists inside fragments
  • Performance β€” Proper keys prevent unnecessary re-renders

Code Examples

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map(todo => (
        <li key={todo.id}>
          <input type="checkbox" checked={todo.completed} readOnly />
          <span style={{ textDecoration: todo.completed ? 'line-through' : 'none' }}>
            {todo.text}
          </span>
        </li>
      ))}
    </ul>
  );
}

// Bad: Using index as key
{items.map((item, index) => (
  <ListItem key={index} item={item} />
))}

// Good: Using stable ID
{items.map(item => (
  <ListItem key={item.id} item={item} />
))}

Practice

Build a sortable list that maintains correct state when items are reordered.

⭐

Premium Content

Keys and Lists

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert React Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement