繁体   English   中英

state 未定义 no - undef 在反应 class 组件中

[英]state is not defined no - undef in react class component

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

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"
  }
}

class 组件是这样的:

import React, { Component } from 'react';

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

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

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

}

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

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

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

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

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

希望它对您也有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM