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