【问题标题】:× TypeError: Cannot read property 'map' of undefined× TypeError:无法读取未定义的属性“地图”
【发布时间】:2020-03-19 15:40:57
【问题描述】:
 function Sidebar({ items, depthStep, depth, expanded }) {
  82 |   return (
  83 |     <div className="sidebar">
> 84 |       <List disablePadding dense>
  85 |         
  86 |         {items.map((sidebarItem, index) => (
  87 |           <React.Fragment key={`${sidebarItem.name}${index}`}>

【问题讨论】:

  • 渲染侧边栏的任何组件都没有传递items 或传递undefined 的值。在 undefined undefined.map() 上尝试 map 会出现此错误。
  • 你能显示项目的定义位置吗?

标签: reactjs dom-events


【解决方案1】:

如果 items 不可避免地被传递给 Sidebar 作为 null 或 undefined,您可以有条件地调用 map 代替:

{
    items ? 
        items.map((sidebarItem, index) => (
            <React.Fragment key={`${sidebarItem.name}${index}`}>
        )
    : null
}

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2019-03-02
    • 1970-01-01
    • 2021-11-17
    • 2022-01-12
    • 2021-02-25
    • 2018-02-08
    • 2019-10-05
    相关资源
    最近更新 更多