简体   繁体   English

React - TypeError:无法读取显示第 36 行的未定义属性“地图”

[英]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:您可以执行以下任一操作:

  1. Set an empty array as the default value for data , like so:将一个空数组设置为data的默认值,如下所示:
export const showDataOnMap = (data = [], caseType = "cases") => 
  1. Or alternatively check whether 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.

相关问题 类型错误:无法读取 React 中未定义的属性“map” - TypeError: Cannot read property 'map' of undefined in React TypeError:无法读取未定义的属性“地图”(反应) - TypeError: Cannot read property 'map' of undefined (React) React-TypeError:无法读取未定义的属性“ map” - React - TypeError: Cannot read property 'map' of undefined React JS Map TypeError:无法读取未定义的属性“地图” - React JS Map TypeError: Cannot read property 'map' of undefined 反应 map “类型错误:无法读取未定义的属性‘映射’” - React map "TypeError: Cannot read property 'map' of undefined" 类型错误:无法读取未定义的属性“地图”,在反应钩子 crud 应用程序中 - TypeError: Cannot read property 'map' of undefined, in react hooks crud application react js-× TypeError:无法读取未定义的属性“映射” - react js-× TypeError: Cannot read property 'map' of undefined React TypeError:在传递道具时无法读取未定义的属性“map” - React TypeError: Cannot read property 'map' of undefined on passing props TypeError:无法读取未定义的属性“ map” React,Redux - TypeError: Cannot read property 'map' of undefined | React, Redux 使用React的新功能:TypeError:无法读取未定义的属性“ map” - New using React: TypeError: Cannot read property 'map' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM