Why do we have to use keys in React?
💡 Model Answer
In React, keys are unique identifiers assigned to elements in a list. They help React's diffing algorithm efficiently determine which items have changed, been added, or removed. Without keys, React would have to re-render the entire list, leading to performance issues and potential bugs in component state. Keys also preserve component instances across re-renders, ensuring that stateful components maintain their state correctly. A common practice is to use a stable, unique value such as an ID from the data source. Avoid using array indices as keys when the list can reorder or items can be inserted/removed, because that can cause incorrect component updates.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500