Javascript React-TypeError:无法读取属性';地图';显示第36行的未定义字段
我不知道我遗漏了什么,但它给了我一个错误“无法读取未定义的属性‘map’。控制台显示它与此部分一起”Javascript React-TypeError:无法读取属性';地图';显示第36行的未定义字段,javascript,reactjs,Javascript,Reactjs,我不知道我遗漏了什么,但它给了我一个错误“无法读取未定义的属性‘map’。控制台显示它与此部分一起” export const showDataOnMap=(数据,caseType=“cases”)=> data.map((国家)=>( {country.country} 案例:{数字(country.Cases).format(“0,0”)} 已恢复:{数字(country.Recovered).format(“0,0”)} 死亡:{数字(国家.死亡数).格式(“0,0”)} )); 您可以执
export const showDataOnMap=(数据,caseType=“cases”)=>
data.map((国家)=>(
{country.country}
案例:{数字(country.Cases).format(“0,0”)}
已恢复:{数字(country.Recovered).format(“0,0”)}
死亡:{数字(国家.死亡数).格式(“0,0”)}
));
您可以执行以下任一操作:
数据的默认值,如下所示:
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
}
>
数据
未定义。。在使用数组方法之前,您必须保证它是一个数组或检查它的存在:data&&data.map
。这是处理异步数据时经常遇到的问题。如果数据
是通过ajax调用填充的,那么它最初可能不是数组。假设数据
是父组件中状态的一部分,只需让它以[]
开头即可
export const showDataOnMap = (data = [], caseType = "cases") =>
data && data.map((country) => (
<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
}
>