在 React 中如何修复这个 TypeError?

How Do I Fix This TypeError In React?

我很挣扎,需要帮助!我对 React 非常陌生 - 我正忙着为我正在学习它的课程做这个练习作业,但我 运行 同样一遍又一遍的问题,我似乎无法正确解决。

我不需要你完成我的作业,但任何提示或方向/反馈 关于我哪里出错以及我做错了什么的代码,我们将不胜感激。哎呀,我已经厌倦了在 Google/forums 上找不到解决方案,而且我的 React 技能相当新手,所以如果没有一些超酷的 React 专家的帮助,我无法真正找出问题所在!非常感谢你提前。 :)

我不断收到错误:类型错误:无法读取未定义的 属性 'map'

Full Source Code Here: https://github.com/christinec-dev/react_new

我的应用程序的总体最终目标如下(出于透视原因在此处添加):

任务 1:

任务 2:

任务 3:

----------------------------------------- menuComponent.js

//package imports
import React, { Component } from 'react';
//import bootrap components from reactstrap library
//NOTE: The media query returns a CSS style 
import { Card, CardImgOverlay, CardImg, CardBody, CardText, CardTitle  } from 'reactstrap';
import DishDetail from './DishdetailComponent';


//creates Menu component
class Menu extends Component {
    
    //define a constructor for it
    constructor(props) {

        //Props is read-only and are used to pass data, whereas state is for managing data and can be modified by its own component

        //required when you create a component in react
        super(props);
        
        //when document is loaded no card has been selected by default
        this.state = {
            selectedDish: null
        };
    }

    //when dishg is clicked, it will render details of dish
    onDishSelect(dish) {
        this.setState({selectedDish: dish});
    }

   //if dish is clicked it will show card details, else nothing
   renderDish(dish) {
    if(dish != null) {
        return(
        <Card>
            <CardImg width="100%" src={dish.image} alt={dish.name} />
                <CardBody>
                    <CardTitle>{dish.name}</CardTitle>
                    <CardText>{dish.description}</CardText>
                </CardBody>
        </Card>
        );
        } else {
            return(
                <div></div>
            )
        }
    }
    
    //return a value or function that will be called
    render() {

        //will iterate (map) over the dishes list and return each key (item) uniquely
        const menu = this.props.dishes.map((dish) => {
            // This will create the layout of the menu items by displaying the image, name and -description- of each menu item
            return (
                <div key={dish.id} className="col-12 col-md-5 m-1">
                      {/* When clicked on, it will run event function*/} 
                      <Card onClick={() => this.onDishSelect(dish.id, dish.comments)}>
                            <CardImg width="100%" src={dish.image} alt={dish.name} />

                            <CardImgOverlay body className="ml-5">
                                <CardTitle>{dish.name}</CardTitle>
                            </CardImgOverlay>
                        </Card>
                </div>
            );
        });

        return (
        //This will return a menu list that will be defined
        <div className="container">
            <div className="row">
                 {/* This will return the menu items */}             
                    {menu}
            </div>
            <div className="row">
                 {/* This will return the clicked card dish items when clicked */} 
                 <div className="col-12 col-md-5 m-1">          
                    {this.renderDish(this.state.selectedDish)},
                </div> 
                <div className="col-12 col-md-5 m-1">          
                    <DishDetail dishes={this.state.dishes} />
                </div>  
            </div>
        </div>
        );
    }
}

//exports it for use in other files
export default Menu;
-------------------DishdetailComponent.js

//package imports
import React, { Component } from 'react';
//import bootrap components from reactstrap library
//NOTE: The media query returns a CSS style 


//creates Dishdetail component
class DishDetail extends Component {
    
    //define a constructor for it
    constructor(props) {
        //Props is read-only and are used to pass data, whereas state is for managing data and can be modified by its own component

        //required when you create a component in react
        super(props);
        
        //when document is loaded no card has been selected by default
        this.state = {
            selectedDish: null,
            comments: null
        };
    }

    onDishSelect(comments) {
        this.setState({comments: comments});
    }

   //if dish is clicked it will show card comments, else nothing
   
    renderComments(comments) {
        if (comments != null){
                return (
                        <ul key={comments.id} className="list-unstyled">
                            <li className="comment">{comments.comment}</li>
                            <li className="author"> {comments.author}</li>
                            <li className="date"> {comments.date}</li>
                        </ul>   
                )       
        }
        else {
            return(
                <div></div>
            )
        }
    }

    //return a value or function that will be called
    render() {

    //will iterate (map) over the dishes list and return each key (item) uniquely
    const details = this.props.comments.map((comments) => {
            return (         
                <div className="container">
                    <div className="row">  
                    <div className="col-12 col-md-5 m-1">
                        <h4>Comments</h4>
                        <div>{comments}</div>  
                    </div>
                    </div>
                </div>
        );
    });

    return (
        //This will return a menu list that will be defined
        <div className="container">
            <div className="row">
                 {/* This will return the menu items */}             
                    {details}
            </div>
            <div className="row">
                 {/* This will return the clicked card dish items when clicked */}             
                 {this.renderComments(this.state.selectedDish)},
            </div>
        </div>
        );
    }
}
  

      
//exports it for use in other files
export default DishDetail;
----------------------------------------- App.js

//package and component imports
import logo from './logo.svg';
import React, {Component} from 'react';
import { NavbarBrand, Navbar } from 'reactstrap';
import Menu from './components/menuComponent';
import DishDetail from './components/DishdetailComponent';

import './App.css';
import {DISHES} from './shared/dishes';

//creates Menu component
class App extends Component {
    
  //define a constructor for it
  constructor (props) {

    //Props is read-only and are used to pass data, whereas state is for managing data and can be modified by its own component

    //required when you create a component in react
    super(props);

   
    //when document is loaded no card has been selected by default
        this.state = {
          dishes: DISHES,
          selectedDish: null,
          comments: null
      };
  }

  //when dishg is clicked, it will render details of dish
  onDishSelect(dish) {
      this.setState({ selectedDish: dish});
      
  }


  render () {
    return (    
    //To create html structures in React we always define it via the className strucutre
    <div>
      
      {/* This will create a layour based on our individual component files. For example, if we have a navbarComponent file, then we just import it from there and insert it here, without having to code the whole thing. */}
      <Navbar color="primary" dark expand="md">
        <div className="container">
        <NavbarBrand href="/"> Ristorante Con Fusion</NavbarBrand>
        </div>
      </Navbar>
  
     {/* The Menu component from the menuComponent.js file is rendered here and displayed when the index.js is loaded */}
     <Menu dishes={this.state.dishes} />
    </div>
  );
  }
}

//exports it for use in other files
export default App;

错误很清楚,this.props.comments 或 this.props.dishes 未定义。

您正在尝试访问未定义元素的 属性.map。

基本上加个检查确保this.props.comments和this.props.dishes存在并且是数组,也可以加个loader也可以加个问题点this.props.comments?.map( ...和 ​​this.props.dishes?.map(...

看了一些文档,我自己解决了。结果我不得不从 menuComponent.js 文件中删除 renderDish(dish) 函数并将其放在 DishdetailComponent.js 文件中。

解决

TypeError: Cannot read property 'map' of undefined problem

我做了以下事情:

 //if dish is clicked it will show dish comments, else nothing
renderComments(array) {
    //if dish array is not equal to null, display comments in half grid
    if (array.length != 0) {
        return (
            //col-12 for sm and xs, and col-5 for md and lg screens with margin @ 1
            <div className="col-12 col-md-5 m-1">
                {/* Displays the comment title, and details of author, date and comment */}
                <h4>Comments</h4>                  
                {/* //will iterate (map) over the comments list and return each key (item) uniquely */}
                { array.map (comment => (
                    <ul className="list-unstyled">
                        <li>
                            <p>{comment.comment}</p>
                            <p><i> - {comment.author} </i>, {comment.date}</p>
                        </li>
                    </ul>
                ))
                }
            </div>
        );
    }
    //if the dish contains nothing, show nothing
    else {
        return (
            <div></div>
        );

    }
}

src\App.js Line 4:8: 'DishDetail' is defined but never used no-unused-vars

搜索关键字以了解有关每个警告的更多信息。 要忽略,请添加 // eslint-disable-next-line to the line before.