如何修复“预期在箭头函数结束时返回一个值"?警告? [英] How do I fix "Expected to return a value at the end of arrow function" warning?

查看:36
本文介绍了如何修复“预期在箭头函数结束时返回一个值"?警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切正常,但我有这个警告预期在箭头函数数组-回调-返回的末尾返回一个值.我尝试使用 forEach 而不是 map,但是 <CommentItem/> 甚至没有显示.我该如何解决这个问题?

 return this.props.comments.map((comment) => {如果(comment.hasComments === true){返回 (<div key={comment.id}><CommentItem className="MainComment"/>{this.props.comments.map(commentReply => {如果(commentReply.replyTo === comment.id){返回 (<CommentItem className="SubComment"/>)//返回}//if 语句})//映射函数}//映射函数 __begin

//评论.id)//返回

解决方案

警告表明您没有在每种情况下都返回地图箭头函数的末尾.

一个更好的方法是首先使用 .filter 然后使用 .map,像这样:

this.props.comments.filter(commentReply => commentReply.replyTo === comment.id).map((commentReply, idx) => );

Everything works fine, but I have this warning Expected to return a value at the end of arrow function array-callback-return. I tried using forEach instead of map, but then <CommentItem /> doesn't even show. How do I fix this?

  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

解决方案

The warning indicates that you're not returning something at the end of your map arrow function in every case.

A better approach to what you're trying to accomplish is first using a .filter and then a .map, like this:

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

这篇关于如何修复“预期在箭头函数结束时返回一个值"?警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆