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
- index as key — Using an item position in an array as its React key, which breaks when items are inserted, deleted or reordered.
- component identity — How React decides that a component in the new render is the same one as before, so it keeps its state.
- reconciliation — The process where React compares the new tree of elements with the previous one and works out the smallest set of changes to the page.
Taught in
- JSX Transpilation: createElement, JSX Runtime, React Elements — React 19 & Modern Component Architecture
- React Fiber Architecture: Work Units, Priority And Commits — React 19 & Modern Component Architecture
- Keys In Depth: Identity Preservation And Identity Resetting — React 19 & Modern Component Architecture
- Diffing Algorithm: Element Type Shifts And Subtree Tear Down — React 19 & Modern Component Architecture
- Avoiding Effect Antipatterns: Derived State And Events — React 19 & Modern Component Architecture
- Rules of Hooks: Call Order, Hook Lists, And Fiber Storage — React 19 & Modern Component Architecture
- useId: Generating Stable Accessible Form Element Identifiers — React 19 & Modern Component Architecture
- Client-Side Data Fetching Architectures with Suspense — React 19 & Modern Component Architecture
- Accessible Headless Primitives: ARIA, Focus, And Keyboard — React 19 & Modern Component Architecture