简体   繁体   English

已定义“片段”,但从未在使用ESLint的React应用中使用no-unused-vars警告

[英]'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.

相关问题 React,定义了警告“css”但从未使用过 no-unused-vars - React, warning 'css' is defined but never used no-unused-vars ESLint:'React' 已定义但从未使用过。(no-unused-vars) 使用 JSX pragma 时 - ESLint: 'React' is defined but never used.(no-unused-vars) when using JSX pragma &#39;FC&#39; 已定义但从未使用过.eslint (no-unused-vars) - 'FC' is defined but never used.eslint (no-unused-vars) 组件已定义但从未使用 reactjs 中的 no-unused-vars 错误警告 - component is defined but never used no-unused-vars error warning in reactjs 警告&#39;ScrollingHorizo​​ntally&#39;已定义,但从未使用过no-unused-vars - warning 'ScrollingHorizontally' is defined but never used no-unused-vars 定义了“标头”,但从未使用过未使用的变量 - 'Header' is defined but never used no-unused-vars &#39;Home&#39; 已定义但从未使用 no-unused-vars&quot;.* - 'Home' is defined but never used no-unused-vars".* 'Form' 已定义但从未使用 no-unused-vars - 'Form' is defined but never used no-unused-vars 第 3:13 行:在 React.js 中使用 Chartjs-2 时定义了“zoom”但从未使用过 no-unused-vars - Line 3:13: 'zoom' is defined but never used no-unused-vars' while using Chartjs-2 in React.js React 声明“'dataTable' 已定义但从未使用过 no-unused-vars”,但我正在使用该变量 - React is stating "'dataTable' is defined but never used no-unused-vars" but I am using the variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM