在使用以下代码创建的javascript文件中:
import React, {Component, Fragment} from "react"
import PropTypes from "prop-types"
export default class DefaultBootstrap extends Component {
render() { return (<div>Hi</div>) }
}
DefaultBootstrap.propTypes = {
name: PropTypes.bool.isRequired
}
当我运行npm start
时,它显示了以下linter错误:
xxx/xxx/index.js
1:1 warning Definition for rule 'prop-types' was not found prop-types
所以我的问题是,即使我在上面定义了prop-types规则,为什么也向我显示以上警告?
我使用Webpack,Babel,React和Eslinter来设置我的应用程序。
.eslintrc.json
{
"parser": "babel-eslint",
"plugins": ["react"],
"rules": {
"prop-types": ["warn"],
}
}
package.json (Click To See Complete File)
{
"devDependencies": {
"@babel/core": "7.1.2",
"babel-eslint": "10.0.1",
"babel-loader": "8.0.4",
"eslint": "5.6.1",
"eslint-loader": "2.1.1",
"eslint-plugin-react": "7.11.1",
"webpack": "4.20.2",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
},
"dependencies": {
"node": "^10.11.0",
"prop-types": "15.6.2",
"react": "16.5.2",
"react-dom": "16.5.2",
"react-scripts": "2.0.3",
}
}
webpack.config.js (Click here To See Complete File)
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader']
},
答案 0 :(得分:2)
在查看您的代码之前,ESLint首先要寻找prop-types
规则的实现。错误消息说它找不到该规则。由于prop-types
规则由插件eslint-plugin-react
提供,并且不是内置规则之一,因此您需要在.eslintrc.json
中用插件名称作为前缀和斜杠:react/prop-types
。这告诉ESLint应该在prop-types
内查找eslint-plugin-react
规则。
{
"parser": "babel-eslint",
"plugins": ["react"],
"rules": {
"react/prop-types": ["warn"],
}
}
您可以在rule configuration docs中阅读更多内容。