一切正常,但是我有这个警告Expected to return a value at the end of arrow function array- callback-return。我尝试使用forEach代替map,但<CommentItem />什至没有显示。我该如何解决?
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
警告表明在每种情况下,您都不会在地图箭头功能的末尾返回任何内容。
一种更好的方法是先使用a .filter然后使用a .map,例如:
.filter
.map
this.props.comments .filter(commentReply => commentReply.replyTo === comment.id) .map((commentReply, idx) => <CommentItem key={idx} className="SubComment"/>);