React Easy technical 1 views 2 min read

What is "key" prop and what is the benefit of using it in arrays of elements?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of React conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

A key is a special attribute you should include when mapping over arrays to render data. _Key_ prop helps React identify which items have changed, are added, or are removed.

Keys should be unique among its siblings. Most often we use ID from our data as _key_:

```jsx harmony
const todoItems = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);


    When you don't have stable IDs for rendered items, you may use the item _index_ as a _key_ as a last resort:

    
jsx harmony
const todoItems = todos.map((todo, index) => (
<li key={index}>{todo.text}</li>
));
```
Benefits of key:

  • Enables React to efficiently update and re-render components.
  • Prevents unnecessary re-renders by reusing components when possible.
  • Helps maintain internal state of list items correctly.

Note:

  1. Using _indexes_ for _keys_ is not recommended if the order of items may change. This can negatively impact performance and may cause issues with component state.
  2. If you extract list item as separate component then apply _keys_ on list component instead of li tag.
  3. There will be a warning message in the console if the key prop is not present on list items.
  4. The key attribute accepts either string or number and internally convert it as string type.
  5. Don't generate the key on the fly something like key={Math.random()}. Because the keys will never match up between re-renders and DOM created everytime.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?