[英]Cardlist is not defined no-undef
I made cardList and want to add card into cardlist through map but this error appears 我制作了cardList,并想通过地图将卡片添加到cardlist中,但出现此错误
import React from 'react';
import Card from './Card';
const Cardlist = ({robots})=>{
const cardComponent = robots.map((user,i) => {
return <Card name = {robots[i].name} id ={robots[i].id} email = {robots[i].email} />
})
return (
<div>
{cardComponent}
</div>
);
}
export default CardList;
You are getting Cardlist is not defined because you didn't export it properly. 您未获得Cardlist,因为您未正确导出Cardlist。
export default CardList;
should become 应该成为
export default Cardlist;
You have defined your component with name Cardlist
and exported with CardList
, a typo here. 您已使用名称
Cardlist
定义了组件,并使用CardList
导出了CardList
的错字。 Your export should be this, 您的出口应该是这样,
export default Cardlist
Also, by the time your component mounts, your robots
array might not contain data and it fails. 另外,在安装组件时,
robots
阵列可能不包含数据,并且会失败。
You should always check if data present, 您应该经常检查是否存在数据,
const cardComponent = robots && robots.length && robots.map((user,i) => {
return <Card name = {robots[i].name} id ={robots[i].id} email = {robots[i].email} />
})
Note: If you already iterating over array and taking each element into user
then you can directly use user
to get data. 注意:如果您已经遍历数组并将每个元素放入
user
则可以直接使用user
获取数据。
const cardComponent = robots && robots.length && robots.map((user,i) => {
return <Card name = {user.name} id ={user.id} email = {user.email} />
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.