为什么不必要的 'else' after 'return' (no-else-return) 错误

Why the Unnecessary 'else' after 'return' (no-else-return) error

我总是使用 else 语句。以及 Data.js 文件中的数据。有什么我可能忽略的吗?

import data from './Data'

function List(props){
  const filteredData = data.filter((el) => {
    if (props.input === '') {
      return el
    } else {
      return el.text.tiLowerCase().includes(props.input)
    }
  })

那是 just what the rule requires

Disallows return before else.

If an if block contains a return statement, the else block becomes unnecessary. Its contents can be placed outside of the block.

如果您认为这条规则值得遵守,请改用(拼写正确)

const filteredData = data.filter((el) => {
    if (props.input === '') {
        return el
    }
    return el.text.toLowerCase().includes(props.input)
})

甚至更好

const filteredData = data.filter((el) => {
    return props.input === ''
        ? el
        : el.text.toLowerCase().includes(props.input)
})

如果 el 永远是真实的,那么 return true 会更有意义。

const filteredData = data.filter((el) => {
    return props.input === ''
        ? true
        : el.text.toLowerCase().includes(props.input)
})

这允许简化为

const filteredData = data.filter((el) => {
    return props.input === '' || !el.text.toLowerCase().includes(props.input)
})