javascript - 状态未定义 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 (
<></>
);
}
}
解决方案
今天也遇到了同样的问题。
react-scripts
看起来像导致该问题的包的依赖项之一-@typescript-eslint/eslint-plugin
已更新到版本 4.15.2(或者它的依赖项之一可能已更新,不确定)。
无论如何,解决方案是更新package.json
并添加这一行:
{
"dependencies": {
"@typescript-eslint/eslint-plugin": "<4.15.2",
...
}
}
然后我完全删除了文件夹node_modules
并运行yarn install
and yarn build
. 或者您将需要运行npm install
and npm build
,具体取决于您的项目。
希望它对您也有帮助。
推荐阅读
- performance - 关于实现高性能汇编指令生成器的建议
- protractor - 如何在量角器中进行错误处理,如未找到元素、元素不可处理、WebDriverError:发生未知错误
- css - Bootstrap 3 输入组插件和输入边框
- ios - 快速禁用xib的屏幕旋转
- php - 达到 Youtube Api 上的许可内容价值 - PHP
- ansible - 如何在ansible上用OR很好地分割多行长条件?
- tensorflow - 如何使用 Keras 加载图像以解决分类问题
- xamarin.forms - 在 Xamarin 表单中调用 DisplayALert() 时出现异常
- c# - NHibernate MappingException:找不到持久类 TodoLog
- reactjs - 从reactjs中的API获取数据后如何导出变量?