Question

In React, is it fine to use indexes for *keys*?

#react

Answer

Not if the order of items may change, as that could reduce the performance and cause issues with component state. That's because component instances are updated and reused based on their key. If the key is an index, moving an item changes it. As a result, component state for things like uncontrolled inputs can get mixed up and updated in unexpected ways.

[^1]: https://reactjs.org/docs/lists-and-keys.html#keys
[^2]: https://reactjs.org/docs/reconciliation.html

Learn

Author

xavi

Reviews
Last
next

Students 1