Home > Web Front-end > JS Tutorial > React Basics~map function/ a list of data~

React Basics~map function/ a list of data~

Susan Sarandon
Release: 2024-10-07 18:17:02
Original
297 people have browsed it
  • When we want to display a list of data, how do we do?

・src/Example.js


const animals = ["Dog", "Cat", "Rat"];

const Example = () => {
  return (
    <>
      <ul>
        {/* Not using the map function. */}
        <li>{animals[0]}</li>
        <li>{animals[1]}</li>
        <li>{animals[2]}</li>
      </ul>
    </>
  );
};

export default Example;


Copy after login
  • This code displays a list of data correctly.

・src/Example.js


const animals = ["Dog", "Cat", "Rat"];

const Example = () => {
  return (
    <>
      <ul>
        {/* Using the map function. */}
        {animals.map((animal) => (
          <li> {animal}</li>
        ))}
      </ul>
    </>
  );
};

export default Example;


Copy after login
  • When we want to display a list of data, the Map function is often used to display an array of data like

  • element.

  • Please don't forget to put the key attribute on the

  • element.

  • This code is cleaner than the previous one.

・This is the console's warning, in case we don't put the key attribute on the

  • element.

    React Basics~map function/ a list of data~

    ・This is the result on the screen.

    React Basics~map function/ a list of data~

    The above is the detailed content of React Basics~map function/ a list of data~. For more information, please follow other related articles on the PHP Chinese website!

    source:dev.to
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Articles by Author
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template