映射時出現(xiàn)以下錯誤:TypeError: comments.map 不是函數(shù)。請幫助我解決以下錯誤。我哪里出錯了,它作為一個組件工作正常,但是一旦我將我的 RenderComments 轉(zhuǎn)換為函數(shù),它就會返回以下錯誤。DishDetail 文件:import React from "react";import { Card, CardImg, CardBody, CardTitle, CardText } from "reactstrap";function RenderDish({ dish }) { return (<Card> <CardImg width="100%" src={dish.image} alt={dish.name} />{" "} <CardBody> <CardTitle> {dish.name} </CardTitle>{" "} <CardText> {dish.description} </CardText>{" "} </CardBody>{" "}</Card> );}function RenderComments(comments) { if (comments != null) {return ( <div> <h4> Comments </h4>{" "} <ul className="list-unstyled"> {" "} {comments.map(comment => { return ( <li key={comment.id}> <p> {comment.comment} </p>{" "} <p> {" "} --{comment.author},{" "} {Intl.DateTimeFormat("en-US", { year: "numeric", month: "short", day: "2-digit" }).format()}{" "} </p>{" "} </li> ); })}{" "} </ul>{" "} </div>); } else {return <div> </div>; }}const DishDetail = props => { if (props.dish != null) {return ( <div className="container"> <div className="row"> <div className="row my-5"> <div className="col-12 col-md-5 m-1"> <RenderDish dish={props.dish} />{" "} </div>{" "} <div className="col-12 col-md-5"> <RenderComments comment={props.dish.comments} />{" "} </div>{" "} </div>{" "} </div>{" "} </div>); } else {return <div> </div>; }};export default DishDetail;
類型錯誤:comments.map 不是 React 中的函數(shù)
嗶嗶one
2023-02-17 10:53:35