Lists and Keys

2020-08-14  本文已影响0人  bestCindy
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
  <li key={number.toString()}>
    {number}
  </li>
);
const todoItems = todos.map((todo, index) => {
      <li key={ index }>
          { todo.text }
      </li>
});
function ListItem(props) {
  const value = props.value;
  return (
    // Wrong! There is no need to specify the key here:
    <li key={value.toString()}>
      {value}
    </li>
  );
}
function NumberList(props) {
  const number = props.numbers;
  const listItems = numbers.map(number => {
    <listItems key={number.toString()} value ={number} />
  });
  return (
    <ul>
      {listItem}
    </ul>
  );
}

we could inline the map() in curly braces

function NumberList(props) {
  const numbers = props.numbers;

  return(
    <ul>
      { numbers.map(number => {
          <ListItem key={number.toString()} value={number} />
      }) } 
    </ul>
  );
}
上一篇下一篇

猜你喜欢

热点阅读