• React之Reacteslint报错(React Hook "useEffect" is called in function "routeClass3" which is neither a React function component or a custom React Hook function reacthooks/rulesofhooks)


     1.eslint去掉注释报错:// eslint-disable-next-line react-hooks/rules-of-hooks

    在使用react hook时会遇到一些问题,就是在使用hook的一些api时就会出现如下所示报错,使用vscode的自动修复就是加上注释,但是每用一次就加一次注释非常麻烦

    问题是:使用组件和props编译报错

    错误信息如下

     React Hook "useEffect" is called in function "xxxxxx" which is neither a React function component or a custom React Hook function  react-hooks/rules-of-hooks

    根本原因 : 因为你的命名不规范,首字母应该是大写

    1. 组件命名大写
    2. interface声明props时需要大写

    其实将命名规范了就可以,如果怕有遗漏导致报错,可以加如下的文件彻底解决

    解决方法

    1.安装依赖

    # npm
    npm install eslint-plugin-react-hooks --save-dev
    
    
    # yarn
    yarn add eslint-plugin-react-hooks --dev

    2.添加一个文件.eslintrc.json

    {
      "env": {
        "browser": true,
        "node": true,
        "es6": true
      },
      "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module",
        "ecmaFeatures": {
          "jsx": true
        }
      },
      "parser": "@typescript-eslint/parser",
      "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/recommended",
        "prettier/@typescript-eslint",
        "plugin:prettier/recommended"
      ],
      "plugins": ["@typescript-eslint", "react", "prettier", "react-hooks"],
      "rules": {
        "prettier/prettier": [
          "error",
          {
            "singleQuote": true
          }
        ],
        "@typescript-eslint/interface-name-prefix": 0,
        "react/prop-types": [0],
        "no-console": "off",
        "eqeqeq": ["error", "always"],
        "jsx-a11y/anchor-is-valid": "off",
        "@typescript-eslint/camelcase": "off",
        "@typescript-eslint/explicit-function-return-type": "error",
        "react-hooks/rules-of-hooks": "error", // 检查 Hook 的规则
        "react-hooks/exhaustive-deps": "warn", // 检查 effect 的依赖
        "no-debugger": "off"
      },
      "settings": {
        "react": {
          "pragma": "React",
          "version": "detect"
        }
      }
    }
  • 相关阅读:
    ssh 配置
    c指针
    centos 配置
    mac terminal 配色
    【webrtc】coturn服务搭建(27)
    【webrtc】SDP(26)
    【webrtc】webrtc通过peerconnection进行音视频互通(25)
    【webrtc】媒体协商的过程(24)
    【webrtc】STUN协议(23)
    【webrtc】webrtc网络传输基本知识(22)
  • 原文地址:https://www.cnblogs.com/fyjz/p/16012045.html
Copyright © 2020-2023  润新知