key (React)

A special string or number you give to elements in a list so React can tell which item is which between renders.

In more detail

React matches old and new children by type and key, so an item keeps its state and DOM node when the list is reordered. Keys must be stable and unique among siblings, such as a database ID. Changing a component key on purpose makes React throw it away and create a fresh one with reset state.

Example

<Profile key={selectedUserId} />

Related terms

Taught in

References