首页 > 解决方案 > 状态未定义 no - 反应类组件中的 undef

问题描述

我在这里找不到任何错误。请帮忙!如果我将它与使用 create-react-app 生成的文件一起使用,我的代码在运行 npm install 时会出错。它工作正常。我不知道缺少哪些依赖项和配置。我收到以下错误:

state is not defined no-undef
foo is not defined no-undef

我的 package.json 是这样的

 {
  "name": "processdesigner",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.8",
    "@testing-library/react": "^11.2.2",
    "@testing-library/user-event": "^12.6.0",
    "axios": "^0.21.1",
    "babel": "^6.23.0",
    "bootstrap": "^4.6.0",
    "classnames": "^2.2.6",
    "i18next": "^19.8.4",
    "i18next-browser-languagedetector": "^6.0.1",
    "i18next-xhr-backend": "^3.2.2",
    "mxgraph": "^4.2.2",
    "prop-types": "^15.7.2",
    "radium": "^0.26.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-i18next": "^11.8.5",
    "react-scripts": "4.0.1",
    "styled-components": "^5.2.1",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.13.0"
  }
}

类组件是这样的:

import React, { Component } from 'react';

class App extends Component{
    state = {
       myState : "stateVal"
    };

    foo = () => {
        console.log("in foo");
    }

   render(){
      return (
         <></>
      );
   }

}

标签: javascriptreactjs

解决方案


今天也遇到了同样的问题。

react-scripts看起来像导致该问题的包的依赖项之一-@typescript-eslint/eslint-plugin已更新到版本 4.15.2(或者它的依赖项之一可能已更新,不确定)。

无论如何,解决方案是更新package.json并添加这一行:

{
  "dependencies": {
    "@typescript-eslint/eslint-plugin": "<4.15.2",
    ...
  }
}

然后我完全删除了文件夹node_modules并运行yarn installand yarn build. 或者您将需要运行npm installand npm build,具体取决于您的项目。

希望它对您也有帮助。


推荐阅读