Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

warning each child in a list should have a unique key prop does not disappear

// incorrect

function ListItem(props) {
  const value = props.value;
  return (
    <li key={value.toString()}>
      {value}
    </li>
  );
}

function NumberList(props) {
  const numbers = props.numbers;
  const listItems = numbers.map((number) =>
    <ListItem value={number} />
  );
  return (
    <ul>
      {listItems}
    </ul>
  );
}

// correct
  const listItems = numbers.map((number) =>
    <ListItem key={number.toString()} value={number} />
  );
 
PREVIOUS NEXT
Tagged: #warning #child #list #unique #key #prop #disappear
ADD COMMENT
Topic
Name
4+5 =