liveBook · Manning

React key

As explained in React Quickly, Second Edition

A key property is a special React property that identifies an element among its siblings so React can track and reuse elements during list rendering.

A key identifies an element among its siblings when React renders a list. React uses it to recognize whether an element moved and reuse it instead of deleting and recreating every element. section 3.2.8

Use keys when mapping data to JSX:

  • Put the key on the root JSX element returned by the map callback. section 3.2.8
  • Choose a unique identifier for the element rather than the array index, because indexes change when elements move. section 3.2.8
  • Keep keys unique among siblings; missing or duplicate keys produce console warnings. section 3.2.8
  • Use the explicit Fragment component when each mapped item returns multiple sibling elements and needs a key. section 3.2.9

Keyed select options

const items = ["apples", "pears", "playstations"]; <select> {items.map((item) => ( <option key={item}>{item}</option> ))} </select> Here, each option uses its item value as a unique key. The key helps React reuse the corresponding option when the list renders again, but it is an internal React property and is not rendered into the DOM. section 3.2.8

Listing 3.12 Definition list of dog breeds
import { Component, Fragment } from "react";
class App extends Component {
  render() {
    const list = [
      { breed: "Chihuahua", description: "Small breed of dog." },
      { breed: "Corgi", description: "Cute breed of dog." },
      { breed: "Cumberland Sheepdog", description: "Extinct breed of dog."},
    ];
    return <Breeds list={list} />;
  }
}
class Breeds extends Component {
  render() {
    return (
      <dl>
        {this.props.list.map(
          ({ breed, description }) => (    #1
            <Fragment key={breed}>         #2
              <dt>{breed}</dt>
              <dd>{description}</dd>
            </Fragment>
          )
        )}
      </dl>
    );
  }
}
export default App;

Browse a keyed Fragment example that renders each dog breed as a paired <dt> and <dd> without adding an extra DOM wrapper. section 3.2.9

How React Quickly, Second Edition covers it

How deep it goes
Explains reuse, uniqueness, index pitfalls, warnings, and keyed Fragments, but stops short of deeper reconciliation internals.
Examples
Uses keyed select options, keyed dog-breed definition entries, and a website menu mapped from objects.
What it assumes
Assumes basic JSX, arrays, and map callbacks while introducing list rendering and keyed Fragments incrementally.
What you can do after
Apply React keys to mapped JSX and choose stable unique identifiers for list elements.
How it connects
Connects list rendering, JSX objects, mapped arrays, DOM reuse, and Fragments.

Also explained in

Compare how 3 books explain React key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.