- 当我们想要显示一个数据列表时,我们该怎么做?
・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;
- 此代码正确显示数据列表。
・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;
当我们想要显示一列数据时,经常会使用map函数来显示像<li></li>元素这样的数组。
请不要忘记将 key 属性放在 <li></li> 元素上。
此代码比上一个更干净。
・这是控制台的警告,以防我们没有将 key 属性放在 <li></li> 元素上。

・这是屏幕上的结果。











