When Index Keys Are Actually Safe
You'll learn: The specific conditions under which using array indices as keys won't cause bugs.
The Exception to the Rule
You've heard "never use index as key," but that's not entirely accurate. There's a safe scenario: when your list is truly static—meaning items never reorder, never get filtered out, and never have items added or removed during the component's lifetime.
Think of it like a photo gallery wall with permanently mounted frames. If those frames never move positions and no new frames are added, numbering them 1, 2, 3 works perfectly fine. The problem only appears when frames start swapping positions or disappearing.
When Static Means Safe
const TUTORIAL_STEPS = [
"Install React",
"Create your first component",
"Learn about props"
];
function Tutorial() {
// This list NEVER changes - safe to use index
return (
<ol>
{TUTORIAL_STEPS.map((step, index) => (
<li key={index}>{step}</li>
))}
</ol>
);
}
This is safe because:
- The array is defined outside the component (or as a constant)
- No user actions can modify it
- The order is permanently fixed
- No filtering or sorting occurs
The Reality Check
Ask yourself: "Could this list ever change?" If you're fetching data, accepting user input, or implementing search/filter/sort features—even in future updates—don't use indices. The bugs they cause are subtle and hard to track down.
For truly hardcoded, display-only lists like navigation items or static tutorial steps, index keys won't hurt you.
Key Takeaway: Index keys are safe only for lists that are completely static and will never be reordered, filtered, or modified—essentially "display constants" rather than dynamic data.