一切正常,但我有一个警告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
发布于 2017-07-10 14:07:04
警告表明,在所有情况下,您都不会在地图箭头函数的末尾返回某些内容。
一种更好的方法是首先使用.filter,然后使用.map,如下所示:
this.props.comments
.filter(commentReply => commentReply.replyTo === comment.id)
.map((commentReply, idx) => <CommentItem key={idx} className="SubComment"/>);发布于 2018-01-14 01:27:13
map()创建一个数组,因此所有代码路径(if/else)都需要一个return。
如果您不想要数组或返回数据,请使用forEach。
发布于 2019-07-26 21:50:35
最简单的方法只有,如果您不需要返回一些东西--它不是只返回return null
https://stackoverflow.com/questions/45014094
复制相似问题