【发布时间】:2017-12-14 07:24:47
【问题描述】:
一切正常,但我收到此警告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
【问题讨论】:
-
不进入if还有没有回报?
-
只有在
commentReply.replyTo === comment.id时才会返回。如果不是这种情况,您将不返回任何内容。只需将return null放在if块之后 -
Mikael Lennholm,我可以说,你是天才
-
@RamzanChasygov 某些语言不允许单分支
if语句,因为它们会导致这样的问题。如果您总是、总是编写任何if语句的else分支,那么您会帮自己一个忙——if代表一个fork 在你的代码中,所以你需要告诉程序每条路径上发生了什么;不只是一个。
标签: javascript reactjs ecmascript-6 redux react-redux