philip brodovsky
philip brodovsky

Reputation: 811

React key prop not must

I have a misunderstanding about the PROP key I manage a list with a delete option I do not use KEY (or sometimes index name as a key) And everything works properly even though I expect problems to happen

Upvotes: 1

Views: 382

Answers (1)

Marc Baumbach
Marc Baumbach

Reputation: 10473

The key prop is used by React internally to track a Component back to its respective virtual DOM element. This helps React do minimal component re-renders, which in turn makes your application more performant. From the React docs on keys:

Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity

The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys

The key prop is not required, but using it with a list will help to significantly reduce the number of times a Component list child is re-rendered.

TL;DR: Always use a unique key for children of a list (e.g., when using map over an array to create components). It'll help React, but isn't necessary for your app to function.

Upvotes: 1

Related Questions