简体   繁体   English

获取未定义的地图错误....使用地图时出现打字错误

[英]Getting undefined map error....Typerror while using the map

I'm getting this "TypeError: Cannot read properties of undefined (reading 'map')"我得到这个“TypeError:无法读取未定义的属性(读取'map')”

const Categories = () => {
    return (
    <Container>
       {categories.map((item) =>(
        <CategoryItem key={item.id}/>
       ))}
    </Container>
  )
}

categories is undefined so the map function is not available.类别未定义,因此地图功能不可用。

if you want to return no category items in this case you can do chained operator to test categories is not null first https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html如果您想在这种情况下不返回任何类别项目,您可以先执行链式运算符来测试类别不为空https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html

 const Categories = () => { return ( <Container> {categories?.map((item) =>( <CategoryItem key={item.id}/> ))} </Container> ) }

这意味着您的categories变量未定义,请确保它确实有一个值并且它是一个有效的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM