![](/img/trans.png)
[英]how to set an useEffect to fetch data from API at first render with eslint-react-hooks?
[英]enabling eslint-react-hooks turns off all eslint rules
我正在嘗試為 eslint 獲取react-hooks
規則,但它似乎不適用於我的設置(VSCode、CRA、airbnb、tslint、更漂亮)。
這是 my.eslintrc.json 在中斷之前:
{
"env": {
"browser": true,
"es6": true,
"jest": true
},
"extends": ["plugin:react/recommended", "airbnb", "prettier"],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2020,
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint"],
"rules": {
"react/jsx-filename-extension": [1, { "extensions": [".jsx", ".tsx"] }]
}
}
這是我的devDependencies
:
"devDependencies": {
"@testing-library/jest-dom": "^5.5.0",
"@testing-library/react": "^10.0.3",
"@testing-library/user-event": "^10.1.0",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.4",
"@types/react": "^16.9.34",
"@types/react-dom": "^16.9.7",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^3.0.0",
"prettier": "^2.0.5",
"typescript": "~3.8.3"
},
然后我嘗試在extends
中添加以下.eslintrc
:
"extends": ["plugin:react/recommended", "plugin:react-hooks/recommended", "airbnb", "prettier"],
我還嘗試將"plugin:react-hooks/recommended"
放在數組中的不同位置。 無論我做什么,它都會破壞所有其他 lint 規則,包括它自己的規則。
我還嘗試將以下內容添加到plugins
和rules
中:
"plugins": ["react", "react-hooks", "@typescript-eslint"],
"rules": {
// ...
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
當然,檢查 lint 規則的測試代碼:
const [a, b] = React.useState("Hey");
React.useEffect(() => {
React.useMemo(() => {}, []);
if (a) b("Yeppp");
if (props.what) console.log(props.what);
exampleMethod();
}, []);
const exampleMethod = () => {};
編輯:忘了提到我也嘗試在extends
中使用airbnb/hooks
:
"extends": ["plugin:react/recommended", "airbnb", "airbnb/hooks", "prettier"],
我找到了解決方案,所以我正在回答我自己的問題。
eslint-plugin-react-hooks文檔說如果使用 Create React App,請確保不要直接在package.json
中添加模塊。
因此,解決方案是從devDependencies
中刪除eslint-plugin-react-hooks
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.