[英]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 语法:
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 :-您可能出错的三种可能性:-
import nav from './nav'
<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;
<nav> <nav/>
But like this但像这样
<Nav> <Nav/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.