当我们想要显示一个数据列表时,我们该怎么做?
・src/example.js
const animals = ["dog", "cat", "rat"];const example = () => { return (
- {/* not using the map function. */}
- {animals[0]}
- {animals[1]}
- {animals[2]}
> );};export default example;</pre>
此代码正确显示数据列表。
・src/example.js
const animals = ["Dog", "Cat", "Rat"];const Example = () => { return (
- {/* Using the map function. */} {animals.map((animal) => (
- {animal}
))}
> );};export default Example;</pre>
当我们想要显示一列数据时,经常会使用map函数来显示像
元素这样的数组。
请不要忘记将 key 属性放在
元素上。
此代码比上一个更干净。
・这是控制台的警告,以防我们没有将 key 属性放在
元素上。

・这是屏幕上的结果。

以上就是React 基础知识~映射函数/数据列表~的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1492423.html
微信扫一扫
支付宝扫一扫