Reactjs TypeError:无法读取属性';地图';“未定义”的定义;反应

Reactjs TypeError:无法读取属性';地图';“未定义”的定义;反应,reactjs,Reactjs,我是React方面的新手,我遵循React的过程。下面的代码出现了类似“TypeError:无法读取未定义的属性“map”的问题”;有人知道发生了什么事吗?非常感谢你 import React, { Component } from 'react' const ListGroup = props => { const {items,textProperty,valueProperty} = props; return ( <ul className=&

我是React方面的新手,我遵循React的过程。下面的代码出现了类似“TypeError:无法读取未定义的属性“map”的问题”;有人知道发生了什么事吗?非常感谢你

import React, { Component } from 'react'

const ListGroup = props => {
    const {items,textProperty,valueProperty} = props;
    return ( 
      <ul className="list-group">
          {items.map(item =>(
         <li key={item[valueProperty]} className="list-group-item">
             {item[textProperty]}
         </li>
          ))}
          
      </ul>
     );
}
 
export default ListGroup;
import React,{Component}来自“React”
const ListGroup=props=>{
const{items,textProperty,valueProperty}=props;
报税表(
    {items.map(item=>(
  • {item[textProperty]}
  • ))}
); } 导出默认列表组;
您的
对象在开始时未定义。只需添加一个空检查:

const ListGroup = props => {
    const {items,textProperty,valueProperty} = props;
    return ( 
      <ul className="list-group">
          {items && items.map(item =>(
               <li key={item[valueProperty]} className="list-group-item">
                 {item[textProperty]}
               </li>
          ))}
          
      </ul>
     );
}
const ListGroup=props=>{
const{items,textProperty,valueProperty}=props;
报税表(
    {items&&items.map(item=>(
  • {item[textProperty]}
  • ))}
); }
您似乎没有将
项目
道具传递到
列表组

<ListGroup items=[] />
或使用elvis运算符:

items?.map(item => {})

您可以使用操作链接,将代码更改为:

const ListGroup = props => {
    const {items,textProperty,valueProperty} = props;
    return ( 
      <ul className="list-group">

          { items?.map(item =>(
               <li key={item[valueProperty]} className="list-group-item">
                 {item[textProperty]}
               </li>
          ))}
          
      </ul>
     );
}
```
const ListGroup=props=>{
const{items,textProperty,valueProperty}=props;
报税表(
    {items?.map(item=>(
  • {item[textProperty]}
  • ))}
); } ```
这意味着
对象尚未设置(未定义的引用)。非常感谢,我还没有发布一个关于堆栈溢出的问题。没问题,如果这有助于您确保接受它作为答案:)编码愉快您为什么不接受我以前的答案,这很有效。你可以投票支持其他有用的答案,但你应该接受最早的有效答案<代码>项目和项目.map和
项目?.map
做同样的事情,如果项目不是Falshi Sinan Yaman,他们会执行映射功能,我很抱歉,我没有注意到我没有接受你的答案。我喜欢你的回答,它对我有效。非常感谢,再次抱歉..非常感谢你的建议和理解!现在我明白发生了什么。。。不会再发生了:)再见!嗨,帕夫洛,非常感谢你的回答,我已经通过了我的另一个文件中的项目:;状态={movies:[],流派:[],页面大小:4,当前页面:1};但是没有用…@yellowcock这个看起来不对劲,你可能想要:items={this.state.genres}你抓到我了,帕夫洛!!非常感谢你!!非常感谢你对Crispen的帮助!如果您觉得有帮助,请投票支持answerHi Crispen,我已经投票了,但是我收到了“感谢反馈!记录了声誉低于15的人的投票,但不要更改公开显示的帖子分数。”我不确定这是否有效。。
const ListGroup = props => {
    const {items,textProperty,valueProperty} = props;
    return ( 
      <ul className="list-group">

          { items?.map(item =>(
               <li key={item[valueProperty]} className="list-group-item">
                 {item[textProperty]}
               </li>
          ))}
          
      </ul>
     );
}
```