简体   繁体   English

无法访问 React 组件中 JavaScript 对象中的数组

[英]Unable to access arrays in JavaScript objects in React Component

I want to access comments array in my DishDetail Component but the compiler is throwing me Uncaught error: comments is not defined.我想访问 DishDetail 组件中的注释数组,但编译器向我抛出未捕获的错误:注释未定义。

 export const DISHES = [ { id: 0, name:'Uthappizza', image: 'assets/images/uthappizza.png', category: 'mains', label:'Hot', price:'4.99', description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', comments: [ { id: 0, rating: 5, comment: "Imagine all the eatables, living in conFusion!", author: "John Lemon", date: "2012-10-16T17:57:28.556094Z" }, { id: 1, rating: 4, comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!", author: "Paul McVites", date: "2014-09-05T17:57:28.556094Z" },

Here is the code snippet for DishDetail Component.这是 DishDetail 组件的代码片段。 When I am running RenderComments component with props.dish.comments, the browser is throwing me an uncaught error: comments is not defined.当我使用 props.dish.comments 运行 RenderComments 组件时,浏览器向我抛出一个未捕获的错误:未定义注释。 I have consoled log and props.dish is grabbing me the information of a particular dish but by typing .comments is no accessing me to comments array.我已经安慰了日志,props.dish 正在为我获取特定菜肴的信息,但通过键入 .comments 并不能访问我的评论数组。

 import React from 'react'; import { Card, CardImg, CardBody, CardText, CardTitle } from 'reactstrap'; function RenderComments({comments}) { if (comments == null) { return (<div></div>) } const cmnts = comments.map(comment => { return ( <li key={comment.id}> <p>{comment.comment}</p> <p>-- {comment.author}, &nbsp; {new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: '2-digit' }).format(new Date(comment.date))} </p> </li> ) }) return ( <div className=''> <h4> Comments </h4> <ul className='list-unstyled'> {cmnts} </ul> </div> ) } function RenderDish({dish}) { if (dish != null) return( <div> <Card key={dish.id}> <CardImg width="100%" src={dish.image} alt={dish.name} /> <CardBody> <CardTitle>{dish.name}</CardTitle> <CardText>{dish.description}</CardText> </CardBody> </Card> </div> ); else return( <div></div> ); } const DishDetail = (props) => { return ( <div className="container"> <div className='row'> <div className="col-12 col-md-5 m-1"> <RenderDish dish={props.dish}/> </div> <div className="col-12 col-md-5 m-1"> <RenderComments comments={props.dish && props.dish.comments}/> {console.log(props.dish.comments)} </div> </div> </div> ) } export default DishDetail

You are passing an empty array.您正在传递一个空数组。 Just pass <RenderComments comments={props.dish}/> .只需通过<RenderComments comments={props.dish}/> And in your function RenderComments use const cmnts = comments.comments.map(comment => {在你的函数RenderComments使用const cmnts = comments.comments.map(comment => {

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

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