简体   繁体   English

src\\App.js 'nav' 已定义但从未使用过 no-unused-vars

[英]src\App.js 'nav' is defined but never used no-unused-vars

Can't create navbar in reactjs.无法在 reactjs 中创建导航栏。

App.js应用程序.js

import './App.css';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css'
import Home from './Routes/Home'
import About from './Routes/About'
import Contact from './Routes/Contact'
import {BrowserRouter, Routes, Route} from 'react-router-dom'
import nav from './nav'


function App() {
  return (
    <div className="App">
      
      <BrowserRouter>
      <nav/>
      
      <Routes>
      <Route>
      
        <Route exact path="/" element={<Home/>} />
        <Route exact path="/about" element={<About/>} />
        <Route exact path="/contact" component={<Contact/>} />
      </Route>
      </Routes>
      </BrowserRouter>
      
    </div>
  );
}

export default App;

nav.js导航.js

import React from 'react'
import {Link} from 'react-router-dom'

function nav(){
    return <div>
      <nav>
          <Link to="/" >Home</Link>
          <Link to="/about" >About</Link>
          <Link to="/contact" >Contact</Link>
      </nav>

    </div>
}

export default nav

The terminals shows " Compiled with warnings. src\\App.js Line 7:8: 'nav' is defined but never used no-unused-vars终端显示“ Compiled with warnings. src\\App.js Line 7:8: 'nav' 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.要忽略,请将 // eslint-disable-next-line 添加到前一行。 "

<nav/> is a valid HTML tag, so ESLint thinks your JSX is referencing that HTML tag, and not the variable that holds the component you imported. <nav/>是一个有效的 HTML 标签,因此 ESLint 认为您的 JSX 正在引用该 HTML 标签,而不是保存您导入的组件的变量。

I recommend renaming your component to be Nav to remove all ambiguity.我建议将您的组件重命名为Nav以消除所有歧义。

From the docs,从文档中,

A variable foo is considered to be used if any of the following are true:如果以下任何一项为真,则认为使用变量 foo:

It is called (foo()) or constructed (new foo())它被调用 (foo()) 或构造 (new foo())

It is read (var bar = foo)它被读取(var bar = foo)

It is passed into a function as an argument (doSomething(foo))它作为参数传递给函数 (doSomething(foo))

It is read inside of a function that is passed to another function (doSomething(function() { foo(); }))它在传递给另一个函数的函数内部读取 (doSomething(function() { foo(); }))

So that, said, you maybe be missing some parsing so your valid jsx and reactjs syntax can be valid eslint syntax:也就是说,您可能会缺少一些解析,因此您的有效 jsx 和 reactjs 语法可以是有效的 eslint 语法:

eslint-plugin-react eslint 插件反应

This happenes because your compiler does not recognize JSX syntax as being used.发生这种情况是因为您的编译器无法识别正在使用的 JSX 语法。

So, You have to install: npm install eslint-plugin-react --save-dev所以,你必须安装: npm install eslint-plugin-react --save-dev

Then in your .eslintrc.json file you should add react/jsx-uses-react然后在你的.eslintrc.json文件中你应该添加react/jsx-uses-react

Three possibilities where you may have been wrong :-您可能出错的三种可能性:-

  1. React shows this error because whenever someone declares a variable but didn't use it. React 会显示此错误,因为每当有人声明变量但未使用它时。 In your case you didn't use this nav in your entire App.js file.在您的情况下,您没有在整个 App.js 文件中使用此导航。
import nav from './nav'
  1. You have forgotten to start the <nav>您忘记启动<nav>
function App() {
  return (
    <div className="App">
      
      <BrowserRouter>
      <nav/>
      
      <Routes>
      <Route>
      
        <Route exact path="/" element={<Home/>} />
        <Route exact path="/about" element={<About/>} />
        <Route exact path="/contact" component={<Contact/>} />
      </Route>
      </Routes>
      </BrowserRouter>
      
    </div>
  );
}

export default App;
  1. Or custom components in react always start with a captial letter, Not like this或者react中的自定义组件总是以大写字母开头,不是这样的
<nav> <nav/>

But like this但像这样

<Nav> <Nav/>

暂无
暂无

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

相关问题 反应错误“ ./src/App.js第7行:&#39;APIKEY&#39;被分配了一个值,但从未使用过no-unused-vars” - React error “./src/App.js Line 7: 'APIKEY' is assigned a value but never used no-unused-vars” 定义了“ stringValues”,但从未使用过no-unused-vars - “stringValues” 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".* &#39;UserInput&#39; 已定义但从未使用过 no-unused-vars &#39;UserOutput&#39; 已定义但从未使用过 no-unused-vars - 'UserInput' is defined but never used no-unused-vars 'UserOutput' is defined but never used no-unused-vars 组件已定义但从未使用 reactjs 中的 no-unused-vars 错误警告 - component is defined but never used no-unused-vars error warning in reactjs Vue.js 错误“app' is assigned a value but never used no-unused-vars - Vue.js error "app' is assigned a value but never used no-unused-vars 如何解决“错误'Vote'已定义但从未使用(no-unused-vars)”的问题? - How to solve the problem “error 'Vote' is defined but never used ( no-unused-vars)”? @typescript-eslint/eslint-plugin 错误:定义了“路由”但从未使用过(no-unused-vars) - @typescript-eslint/eslint-plugin error: 'Route' is defined but never used (no-unused-vars) &#39;inStock&#39; 被赋值但从未使用过 no-unused-vars - 'inStock' is assigned a value but never used no-unused-vars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM