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

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

TypeError: comments.map 不是函數 react js

TypeError: comments.map 不是函數 react js

12345678_0001 2023-04-14 17:14:35
當我選擇一個對象時出現錯誤,當前評論文件存儲在另一個文件中,我正在這個文件中訪問它,但出現錯誤TypeError: comments.map 不是一個函數我的代碼:import React from "react";import { Card, CardImg, CardImgOverlay, CardText, CardBody, CardTitle } from "reactstrap";function RenderDish({ dish }) {  if (dish != null) {    return (      <div className="col-12 col-md-5 m-1">        <Card>          <CardImg width="100%" object src={dish.image} alt={dish.name}></CardImg>          <CardBody>            <CardTitle>{dish.name}</CardTitle>            <CardText>{dish.description}</CardText>          </CardBody>        </Card>      </div>    );  } else {    return <div></div>;  }}function RenderComments(comments) {  if (comments != null) {    const commentsList = comments.map((Comment) => {      return (        <div className="container">          <li key={Comment.id}>            <p>{Comment.Comment}</p>            <p>              -- {Comment.author},              {new Intl.DateTimeFormat("en-US", { year: "numeric", month: "short", day: "2-digit" }).format(new Date(Date.parse(Comment.id)))}            </p>          </li>        </div>      );    });    return (      <div className="col-12 col-md-5 m-1">        <h3>Comments</h3>        <ul className="list-unstyled">{commentsList}</ul>      </div>    );  } else {    return <div></div>;  }}const DishDetail = (props) => {  console.log("Dishdetail Component render invoked");  if (props.dish != null) {    return (      <div className="row">        <RenderDish dish={props.dish} />        <RenderComments comments={props.dish.comments} />      </div>    );  } else {    return <div></div>;  }};export default DishDetail;
查看完整描述

3 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

我得到了同樣的錯誤。


在我的例子中,通過在主要組件中選擇索引 0 而不是完整數組,錯誤地傳遞了單個數組項。


comments={this.state.comments.filter((comment) => comment.dishId === parseInt(match.params.dishId, 10))[0]}在主組件中。


更正版本:


const DishWithId = ({ match }) => {

        return (

            <DishDetail dish={this.state.dishes.filter((dish) => dish.id === parseInt(match.params.dishId, 10))[0]}

                comments={this.state.comments.filter((comment) => comment.dishId === parseInt(match.params.dishId, 10))} />

        );

    }


查看完整回答
反對 回復 2023-04-14
?
慕的地8271018

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

我很抱歉這個答案是矯枉過正。


你傳入的任何 like 都comment={comment}將作為對象成為 props 的一部分,因此你需要從中解構評論。


您也不需要在條件渲染期間返回空的 div。什么都不返回是一樣的。


還有,value != null不夠。如果該值是任何其他類型的假值,您的應用程序仍會崩潰,因此!value更安全。


如果您要有條件地內聯渲染,則使用&&or 三元? :而不是標準if.


import React from "react";

import { Card, CardImg, CardImgOverlay, CardText, CardBody, CardTitle } from "reactstrap";


const RenderDish = ({ dish }) => (

  dish && (

    <div className="col-12 col-md-5 m-1">

      <Card>

        <CardImg width="100%" object src={dish.image} alt={dish.name}></CardImg>

        <CardBody>

          <CardTitle>{dish.name}</CardTitle>

          <CardText>{dish.description}</CardText>

        </CardBody>

      </Card>

    </div>

  )

);


const RenderComments = ({ comments }) => (

  comments && (

    <div className="col-12 col-md-5 m-1">

      <h3>Comments</h3>

      <ul className="list-unstyled">{

        comments.map(comment => (

          <div className="container">

            <li key={comment.id}>

              <p>{comment.comment}</p>

              <p>

                -- {comment.author},

                {new Intl.DateTimeFormat("en-US", {

                  year: "numeric",

                  month: "short",

                  day: "2-digit"

                }).format(new Date(Date.parse(comment.id)))}

              </p>

            </li>

          </div>

        ))

      }</ul>

    </div>

  )

);


const DishDetail = ({ dish }) => (

  dish && (

    <div className="row">

      <RenderDish dish={dish} />

      <RenderComments comments={dish.comments} />

    </div>

  )

);


export default DishDetail;


查看完整回答
反對 回復 2023-04-14
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

您傳遞道具并映射道具而不是訪問它:


<RenderComments comments={props.dish.comments} />


// Not RenderComments(comments)

function RenderComments(props) {

  ...

  props.comments.map(...)

}


查看完整回答
反對 回復 2023-04-14
  • 3 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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