我用npx create-react-app my-app创建了一个react应用程序,并进行了纱线安装。在App.js中添加了以下内容:
const a = () => 1,
b = () => 2,
c = () => 3;
function App() {
const wut = useMemo(() => {
return { a: a(), b: b(), c: c() };
}, [a]);
useEffect(() => {
console.log(a(), b(), c());
}, [a]);纱线开始会给我警告,但vscode没有。
我添加了具有以下内容的.eslintrc.js:
module.exports = {
env: {
browser: true,
es6: true
},
extends: ["eslint:recommended", "plugin:react/recommended"],
globals: {
Atomics: "readonly",
SharedArrayBuffer: "readonly"
},
parserOptions: {
ecmaFeatures: {
jsx: true
},
ecmaVersion: 2018,
sourceType: "module"
},
plugins: ["react"],
rules: {
"react/prop-types": [0],
"no-console": [0],
"react-hooks/exhaustive-deps": "warn"
}
};发布于 2019-09-14 17:16:50
如果我在一个干净的创建反应应用程序中有以下代码:
function App(props) {
const {a,b,c}=props;
const wut = useMemo(() => {
console.log("this should not happen multiple times");
return { a: a(), b: b(), c: c() };
}, [a]);
useEffect(() => {
console.log(a(), b(), c());
}, [a]);然后删除.eslintrc.js将给我React Hook useMemo has missing dependencies:警告和自动修复它的选项。
当我将.eslintrc.js放在项目根目录中时,我得到了错误Definition for rule 'react-hooks/exhaustive-deps' was not found.,这是因为.eslintrc.js丢失了一个插件,plugins: ["react", "react-hooks"],解决了它。
但是,现在该规则默认关闭,因此它不会发出警告,需要显式地添加规则并将其设置为.eslintrc.js中的警告(或错误):
rules: {
"react-hooks/exhaustive-deps": "error"
}发布于 2019-09-14 16:37:57
react/recommended不包括react-hooks/exhaustive-deps。
您可能没有安装eslint插件-反应-挂钩。
其中包括一份说明:
注意:如果您使用的是Create,请等待相应版本的React -包含此规则的脚本,而不是直接添加它。
你用的是什么版本的c a?理论上,它是通过这个公关添加的。
发布于 2019-09-24 18:55:23
我使用的是TypeScript,所以我的答案是在我的VSCode设置中添加以下内容:
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],https://stackoverflow.com/questions/57937336
复制相似问题