Why are keys important in ReactJS?

A key is a special string attribute that needs to be included when using lists of elements.

  • Keys help react identify which elements were added, changed or removed.
  • Keys should be given to array elements for providing a unique identity for each element.
  • Without keys, React does not understand the order or uniqueness of each element.
  • With keys, React has an idea of which particular element was deleted, edited, and added.
  • Keys are generally used for displaying a list of data coming from an API.