亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

類型錯誤:comments.map 不是 React 中的函數

類型錯誤:comments.map 不是 React 中的函數

嗶嗶one 2023-02-17 10:53:35
映射時出現以下錯誤:TypeError: comments.map 不是函數。請幫助我解決以下錯誤。我哪里出錯了,它作為一個組件工作正常,但是一旦我將我的 RenderComments 轉換為函數,它就會返回以下錯誤。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;
查看完整描述

1 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

功能組件的默認參數被命名props,可以被破壞以獲取傳遞的道具,如:

   function RenderComments({comments}) {
      ....


查看完整回答
反對 回復 2023-02-17
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號