・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> 元素上。

・这是屏幕上的结果。

以上就是React 基础知识~映射函数/数据列表~的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号