首页 > 解决方案 > 应用程序不会运行 TypeError:无法读取未定义的属性“地图”

问题描述

我一直在地图上遇到这个问题,但无法弄清楚原因。

export default function() {
  return (
    <AppContext.Consumer>
      {({ prices }) => (
        <PriceGrid>
          {prices.map((price, index) => (
            <PriceTile key={`priceTile-${index}`} index={index} price={price}/>
          ))}
        </PriceGrid>
      )}
    </AppContext.Consumer>
  );
}

标签: reactjs

解决方案


最有可能的价格在某些时候不是数组...您可以进行类型检查,如果价格是数组以外的任何类型,它将跳过;

      {Array.isArray(prices) && prices.map((price, index) => (
        <PriceTile key={`priceTile-${index}`} index={index} price={price}/>
      ))}

推荐阅读