如何修复"Expected to return a value at the end of arrow function."错误?



Eslint给了我期望在箭头函数错误结束时返回一个值。我该如何解决这个问题?

useEffect(() => {
if (show && inView) {
const timer = () => {
setCount(count + 1);
};
if (count >= number) {
const plus = setPlus('+');
return plus;
}
const interval = setInterval(timer, 800 / number);
return () => clearInterval(interval);
}
setCount(1);
setPlus('');
setShow(false);
}, [count, inView, number, plus, show]);

你是否在项目中使用了一些eslint的react扩展?

这是react项目的标准eslint配置:

module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'plugin:react/recommended',
'airbnb',
],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: 'module',
},
plugins: [
'react',
'prefer-arrow',
],
}

在你的。eslintrc中试试,如果你仍然得到这个错误,告诉我

编辑:

你可以加入eslint配置文件吗?你不应该得到这个错误。也许我可以给你解释一下。或者你可以通过编辑.eslintrc文件并添加:

来禁用此规则。

rules: {
'consistent-return': 'off',
}

这是我的。eslintrc

{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"react-app",
"plugin:react/recommended",
"airbnb",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["react", "prettier"],
"rules": {
"react/jsx-uses-react": ["off"],
"react/react-in-jsx-scope": ["off"],
"react/jsx-props-no-spreading": ["warn"],
"no-shadow": "off"
}

}

相关内容

最新更新