简体   繁体   English

如何修复“预期在箭头函数末尾返回一个值”警告?

[英]How do I fix "Expected to return a value at the end of arrow function" warning?

Everything works fine, but I have this warning Expected to return a value at the end of arrow function array-callback-return .一切正常,但我有这个警告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.我尝试使用forEach而不是map ,但是<CommentItem />甚至没有显示。 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

A map() creates an array, so a return is expected for all code paths (if/elses). map()创建一个数组,因此所有代码路径(if/elses)都需要return

If you don't want an array or to return data, use forEach instead.如果您不需要数组或返回数据,请改用forEach

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:一个更好的方法来完成你想要完成的是首先使用.filter然后.map ,就像这样:

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

The easiest way only if you don't need return something it'ts just return null当您不需要返回某些东西时,最简单的方法不只是return null

The problem seems to be that you are not returning something in the event that your first if -case is false.问题似乎是,如果您的第一个if -case 为假,您不会返回任何东西。

The error you are getting states that your arrow function (comment) => { doesn't have a return statement.您收到的错误表明您的箭头函数(comment) => {没有返回语句。 While it does for when your if -case is true, it does not return anything for when it's false.当您的if -case 为真时它会执行此操作,但当它为假时它不会返回任何内容。

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"/>
            )
          }
        })
        }
      </div>
    )
  } else {
     //return something here.
  }
});

edit you should take a look at Kris' answer for how to better implement what you are trying to do.编辑你应该看看 Kris 的回答,了解如何更好地实现你正在尝试做的事情。

The most upvoted answer, from Kris Selbekk, it is totally right.来自 Kris Selbekk 的投票最多的答案是完全正确的。 It is important to highlight though that it takes a functional approach, you will be looping through the this.props.comments array twice, the second time(looping) it will most probable skip a few elements that where filtered, but in case no comment was filtered you will loop through the whole array twice.重要的是要强调虽然它采用了函数式方法,但您将遍历this.props.comments数组两次,第二次(循环)它很可能会跳过一些被过滤的元素,但如果没有comment被过滤后,您将遍历整个数组两次。 If performance is not a concern in you project that is totally fine.如果您的项目不关心性能,那完全没问题。 In case performance is important a guard clause would be more appropriated as you would loop the array only once:如果性能很重要,则guard clause会更合适,因为您只会循环数组一次:

return this.props.comments.map((comment) => {
  if (!comment.hasComments) return null; 

  return (
    <div key={comment.id}>         
      <CommentItem className="MainComment"/>
        {this.props.comments.map(commentReply => {             
          if (commentReply.replyTo !== comment.id) return null;

          return <CommentItem className="SubComment"/>
        })} 
    </div>          
  ) 
}

The main reason I'm pointing this out is because as a Junior Developer I did a lot of those mistakes(like looping the same array multiple times), so I thought i was worth mention it here.我指出这一点的主要原因是因为作为一名初级开发人员,我犯了很多这样的错误(比如多次循环同一个数组),所以我认为我在这里值得一提。

PS: I would refactor your react component even more, as I'm not in favour of heavy logic in the html part of a JSX , but that is out of the topic of this question. PS:我会更多地重构你的反应组件,因为我不赞成JSXhtml part中的大量逻辑,但这不在这个问题的主题之内。

You can use the for loop like so:您可以像这样使用 for 循环:

for(let i = 0 ; i < comments.length; i++){
 if(comments[i].hasComments === true){
return (
       <div key={comments[i].id}>
        //content Here
      </div> // comment.id
        )
      }
     }

 class Blog extends Component{ render(){ const posts1 = this.props.posts; //console.log(posts) const sidebar = ( <ul> {posts1.map((post) => { //Must use return to avoid this error. return( <li key={post.id}> {post.title} - {post.content} </li> ) }) } </ul> ); const maincontent = this.props.posts.map((post) => { return( <div key={post.id}> <h3>{post.title}</h3> <p>{post.content}</p> </div> ) }) return( <div>{sidebar}<hr/>{maincontent}</div> ); } } const posts = [ {id: 1, title: 'Hello World', content: 'Welcome to learning React,'}: {id, 2: title, 'Installation': content. 'You can install React from npm;'} ]. ReactDOM,render( <Blog posts={posts} />. document;getElementById('root') );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复预期在反应js中的箭头function警告末尾返回值? - How to fix expected to return a value at the end of arrow function warning in react js? 预期在 reactjs 上的箭头函数末尾返回一个值如何修复此错误? - Expected to return a value at the end of arrow function on reactjs how to fix this error? 如何修复警告“预计在箭头 function 数组回调返回中返回一个值” - How fix warning “Expected to return a value in arrow function array-callback-return” 为什么我收到此警告 159:35 警告预计将在箭头 function 数组回调返回的末尾返回一个值? - Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return? 预期返回箭头中的值 function 警告 - Expected to return a value in arrow function warning 预期在react中的箭头函数末尾返回一个值 - Expected to return a value at the end of arrow function in react 重构:期望在箭头函数的末尾返回一个值 - Refactor: Expected to return a value at the end of arrow function 预计在箭头 function 的末尾使用 if 语句返回一个值 - Expected to return a value at the end of arrow function with if statement 反应警告 预期在箭头函数数组-回调-返回结束时返回一个值 - react warning Expected to return a value at the end of arrow function array-callback-return 错误预期在箭头函数的末尾返回一个值 - Error Expected to return a value at the end of arrow function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM