[英]'Fragment' is defined but never used no-unused-vars warning with React app using ESLint
I am fairly new to React and I've created a new app using the create-react-app
tool and started to play around with it. 我对React还是相当陌生,并且已经使用
create-react-app
工具创建了一个新应用,并开始使用它。 When ever I run my app, I get following warning: 每当我运行我的应用程序时,都会收到以下警告:
Compiled with warnings.
./src/index.js
Line 1: 'Fragment' is defined but never used no-unused-vars
Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.
The following code is in my App.js : 以下代码在我的App.js中 :
import React, { Component, Fragment } from "react";
import Header from "./Layouts/Header";
import Footer from "./Layouts/Footer";
export default class extends Component {
render() {
return (
<Fragment>
<Header />
<p>Hello World</p>
<Footer />
</Fragment>
);
}
}
Which is used like this in my index.js (main entry point): 在我的index.js (主入口点)中这样使用:
import React, { Fragment } from "react";
import ReactDOM from "react-dom";
import registerServiceWorker from "./registerServiceWorker";
import App from "./Components/App";
ReactDOM.render(<App />, document.getElementById("root"));
registerServiceWorker();
And I have installed the react plugin for ESLint and this is the configuration I am using in my .eslintrc 我已经为ESLint安装了react插件,这是我在.eslintrc中使用的配置
{
"plugins": ["react"],
"rules": {
"react/jsx-uses-react": 2
},
"extends": ["eslint:recommended", "plugin:react/recommended"]
}
Any ideas how to resolve this warning? 任何想法如何解决此警告?
You have the line 你有线
import React, { Fragment } from "react";
in your index.js
file, but you aren't using Fragment
anywhere in that file. 在
index.js
文件中,但是在该文件的任何地方都没有使用Fragment
。 You can fix this by changing the import line to: 您可以通过将导入行更改为以下内容来解决此问题:
import React from "react";
Edit: Note the identical import
in your App.js
file is perfectly valid, as you are using Fragment
in that file. 编辑:请注意,在
App.js
文件中的相同import
是完全有效的,因为您正在该文件中使用Fragment
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.