how to add key property react child code example

Example 1: react component key prop

function ListItem(props) {
  // Correct! There is no need to specify the key here:  return <li>{props.value}</li>;}

function NumberList(props) {
  const numbers = props.numbers;
  const listItems = numbers.map((number) =>
    // Correct! Key should be specified inside the array.    <ListItem key={number.toString()} value={number} />  );
  return (
    <ul>
      {listItems}
    </ul>
  );
}

const numbers = [1, 2, 3, 4, 5];
ReactDOM.render(
  <NumberList numbers={numbers} />,
  document.getElementById('root')
);

Example 2: react list based on state

import React, { Component } from 'react'; class App extends Component {  constructor(props) {    super(props);     this.state = {      list: [        { id: '1', age: 42 },        { id: '2', age: 33 },        { id: '3', age: 68 },      ],    };  }   onRemoveItem = id => {    this.setState(state => {      const list = state.list.filter(item => item.id !== id);       return {        list,      };    });  };   render() {    return (      <div>        <ul>          {this.state.list.map(item => (            <li key={item.id}>              The person is {item.age} years old.              <button                type="button"                onClick={() => this.onRemoveItem(item.id)}              >                Remove              </button>            </li>          ))}        </ul>      </div>    );  }} export default App;