小编典典

如何修复“预期在箭头函数的结尾处返回值”警告?

reactjs

一切正常,但是我有这个警告Expected to return a value at the end of arrow function array- callback-return。我尝试使用forEach代替map,但<CommentItem />什至没有显示。我该如何解决?

  return this.props.comments.map((comment) => {

      if (comment.hasComments === true) {

        return (
          <div key={comment.id}>

            <CommentItem className="MainComment"/>

              {this.props.comments.map(commentReply => {

                if (commentReply.replyTo === comment.id) { 
                  return (
                    <CommentItem className="SubComment"/>
                 ) // return
                } // if-statement
              }) // map-function
              } // map-function __begin

          </div> // comment.id

        ) // return

阅读 827

收藏
2020-07-22

共1个答案

小编典典

警告表明在每种情况下,您都不会在地图箭头功能的末尾返回任何内容。

一种更好的方法是先使用a .filter然后使用a .map,例如:

this.props.comments
  .filter(commentReply => commentReply.replyTo === comment.id)
  .map((commentReply, idx) => <CommentItem key={idx} className="SubComment"/>);
2020-07-22