期望在reactjs上的箭头函数结尾处返回值如何解决此错误?

时间:2020-09-17 08:34:13

标签: javascript reactjs

const items = this.state.events
  .filter((data) => {
    if (this.state.search == null) return data;
    else if (
         ...
         ...
    ) {
      return data;
    }
  })
  .map((data) => {
    return (
      <div>
            <span>{data.title}</span>
            <span>{data.description}</span>
      </div>
    );
  });

这是我的函数,我尝试了不同的放回车符的方式,但仍然出现错误预期在箭头函数的末尾返回一个值。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

对于逻辑的每个分支,您都应该有一个有效的回报

.filter((data) => {
  if (this.state.search == null) {
    return data; // <-- branch 1 good
  } else if (...) {
    return data; // <-- branch 2 good
  } // implicit else
  // <-- oops, branch 3 missing, add explicit return
  return true; 
})