[英]React - TypeError: Cannot read property 'map' of undefined showing line 36
I don't see what I'm missing but it's giving me an error of "Cannot read property 'map' of undefined. The console is showing it's with this section"我没有看到我错过了什么,但它给了我一个错误“无法读取未定义的属性‘地图’。控制台显示它与此部分有关”
export const showDataOnMap = (data, caseType = "cases") =>
data.map((country) => (
<Circle
center={[country.countryInfo.lat, country.countryInfo.long]}
color={caseTypeColors[caseType].hex}
fillColor={caseTypeColors[caseType].hex}
fillOpacity={0.4}
radius={
Math.sqrt(country[caseType]) * caseTypeColors[caseType].multiplier
}
>
<Popup>
<div className="info-container">
<div
className="info-flag"
style={{ backgroundImage: `url(${country.countryInfo.flag})` }}
></div>
<div className="info-name">{country.country}</div>
<div className="info-confirmed">
Cases: {numeral(country.cases).format("0,0")}
</div>
<div className="info-recovered">
Recovered: {numeral(country.recovered).format("0,0")}
</div>
<div className="info-deaths">
Deaths: {numeral(country.deaths).format("0,0")}
</div>
</div>
</Popup>
</Circle>
));
You can do either of these:您可以执行以下任一操作:
data
, like so:data
的默认值,如下所示:export const showDataOnMap = (data = [], caseType = "cases") =>
data
is defined before using it:data
是否已定义:data && data.map((country) => (
Also side note - remember to put keys in the returned element when you are using map
:另请注意 - 请记住在使用
map
时将键放在返回的元素中:
<Circle
key={} // <-- add some unique value from the item here
center={[country.countryInfo.lat, country.countryInfo.long]}
color={caseTypeColors[caseType].hex}
fillColor={caseTypeColors[caseType].hex}
fillOpacity={0.4}
radius={
Math.sqrt(country[caseType]) * caseTypeColors[caseType].multiplier
}
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.