Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ React Mastery Series β€” Day 21

Updated
β€’3 min read
πŸ“˜ React Mastery Series β€” Day 21
S

Frontend Developer | React & JavaScript | Building production-ready web apps Developer with 1.5+ years of experience, currently focused on React, Redux, and full-stack development. Sharing practical tutorials, concepts, and project learnings.

Lists & Keys in React β€” Dynamic Rendering Mastery

In real applications, we rarely hardcode UI.

We display:

  • User lists

  • Products

  • Comments

  • Notifications

  • Orders

All of this is done using Lists in React.

But, there’s one important thing attached to lists…

πŸ‘‰ Keys

Today we understand both properly.


🧠 Rendering Lists in React

In React, we render lists using JavaScript’s .map() method.


πŸ›  Example β€” Basic List Rendering

function UserList() {
  const users = ["Shailaja", "Aman", "Priya", "Rahul"];

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

βœ” .map() loops over array

βœ” Returns JSX

βœ” Renders dynamic content


πŸ€” What Are Keys in React?

Keys are unique identifiers used by React to track which list items change, are added, or are removed.

React uses keys for efficient re-rendering.

Without keys, React shows warning.


🧠 Why Keys Are Important?

Imagine:

  • You remove item 2 from list

  • React must know which item changed

Keys help React:

βœ” Identify elements

βœ” Optimize re-render

βœ” Avoid UI bugs


⚠ Common Mistake

Using index as key.

<li key={index}>{user.name}</li>

This works…

But not recommended when:

  • List items can be reordered

  • Items can be deleted

  • Data changes dynamically


βœ… Best Practice β€” Use Unique ID

function UserList() {
  const users = [
    { id: 1, name: "Shailaja" },
    { id: 2, name: "Aman" },
    { id: 3, name: "Priya" }
  ];

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

βœ” Stable

βœ” Predictable

βœ” Professional practice


πŸ”₯ Real-World Example β€” Rendering Products

function ProductList({ products }) {
  return (
    <div>
      {products.map((product) => (
        <div key={product.id}>
          <h3>{product.name}</h3>
          <p>β‚Ή{product.price}</p>
        </div>
      ))}
    </div>
  );
}

This pattern is used in:

  • E-commerce apps

  • Dashboards

  • Admin panels

  • Social media feeds


🧠 Interview One-Liner

Keys in React are unique identifiers used to help React efficiently update and re-render list items.


🎯 Rules to Remember

βœ” Always provide a key

βœ” Key must be unique

βœ” Prefer stable IDs

βœ” Avoid index when possible


πŸ”— How It Connects with Previous Days

βœ” Conditional Rendering (Day 20)

βœ” Lifting State (Day 19)

βœ” Forms (Day 18)

βœ” Real API data rendering

Now you can confidently render dynamic UI.


βœ… Key Takeaways

βœ” Use .map() to render lists

βœ” Keys are mandatory

βœ” Use unique IDs

βœ” Avoid index when list is dynamic


πŸ”œ Coming Next

Day 22 β€” Handling Events in React (Clean & Scalable Event Logic)

Now we refine interaction patterns πŸ”₯

More from this blog