简体   繁体   English

'Home' 已定义但从未使用 no-unused-vars".*

[英]'Home' is defined but never used no-unused-vars".*

I'm new to React.我是反应新手。

I've made a component called Avatar which I want to link to a home page if clicked.我制作了一个名为 Avatar 的组件,如果单击它,我想链接到主页。

* When compiling I get the following error: "Line 9:12: 'Home' is defined but never used no-unused-vars". *编译时出现以下错误:“Line 9:12: 'Home' is defined but never used no-unused-vars”。

So, my question is how do I correctly link the Avatar Icon to Home that's declared in ./Auth.js ?所以,我的问题是如何正确地将头像图标链接到在 ./Auth.js 中声明的主页?

BookList.js Here is my current code. BookList.js这是我当前的代码。

 import React from "react"; import Book from "./Book"; import { BookContext } from "../context/BookContext"; import { ThemeContext } from "../context/ThemeContext"; import "./index.css"; import "./BookList.css"; import Avatar from "./Avatar" import {BrowserRouter, Route} from "react-router-dom"; import Home from "./Auth"; const RouteAvatarClick = () => { <BrowserRouter> <Route exact path="/Home"> //Tried to link Home from ./Auth <Avatar /> </Route> </BrowserRouter> } export default class BookList extends React.Component { render() { return ( <ThemeContext.Consumer> {(contextTheme) => ( <BookContext.Consumer> {(contextBook) => { const { books } = contextBook; const { changeColorTheme, isDarkMode, dark, light} = contextTheme; const theme = isDarkMode ? dark : light; //Conditional return ( <section className="page-section" style={{ backgroundColor: theme.bg, color: theme.color }} id="portfolio"> <div className="container"> <div><RouteAvatarClick/></div> <div className="text-right"><button className="btn btn-danger" onClick={changeColorTheme}>Change Mood</button></div> <div className="text-center"> <h2 className="section-heading text-uppercase">My Book Folio</h2> <h3 className="section-subheading text-muted">Books I would like to read.</h3></div> <div className="row"> {books.map((book, index) => { return <Book book={book} key={index} />; })} </div> </div> </section> ); }} </BookContext.Consumer> )} </ThemeContext.Consumer> ); } }

Avatar.jsx阿凡达.jsx

 import React from 'react'; import "./Avatar.css"; const Avatar = ({className, src, alt, ...props}) => { return ( <div> {src? ( <img className={`defaultClass ${className}`} src={src} alt={alt} /> ) : ( <img {...props} //If we want to use other images className={`defaultClass ${className}`} src={"https://i.pinimg.com/736x/49/f7/25/49f725a9f2b62ea80603f3fe51289735--le-design-icon-design.jpg"} alt={alt} /> )} </div> ); } export default Avatar

Auth.js Auth.js

 import React from "react"; const Home = () => ( <div> <h1 className="title is-1">This is the Home Page</h1> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras gravida, risus at dapibus aliquet, elit quam scelerisque tortor, nec accumsan eros nulla interdum justo. Pellentesque dignissim, sapien et congue rutrum, lorem tortor dapibus turpis, sit amet vestibulum eros mi et odio. </p> </div> ); export default Home;

Please don't hesitate to let me know if there's a way to improve my question请不要犹豫,让我知道是否有办法改善我的问题

Currently in your RouteAvatarClick component, you've provided the path ("/Home"), but not the element/component to render when the user is on that route.目前在您的RouteAvatarClick组件中,您提供了路径(“/Home”),但没有提供当用户在该路线上时要呈现的元素/组件。 Try something like this in your RouteAvatarClick component:RouteAvatarClick组件中尝试这样的操作:

<Route exact path="/Home" element={<Home />}>

Then navigate to /Home in your browser ( http:localhost:3000/Home ) and see if that helps!然后在浏览器中导航到/Home ( http:localhost:3000/Home ) 看看是否有帮助! <3 <3

PS The element prop is assuming you're using react-router v6. PS element属性假设您使用的是 react-router v6。 If you're using v5, it'll be component={Home} .如果您使用的是 v5,它将是component={Home}

暂无
暂无

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

相关问题 定义了“ stringValues”,但从未使用过no-unused-vars - “stringValues” is defined but never used no-unused-vars 定义了“标头”,但从未使用过未使用的变量 - 'Header' 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 src\\App.js &#39;nav&#39; 已定义但从未使用过 no-unused-vars - src\App.js 'nav' is defined 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 11:5 错误“路由器”被分配了一个值,但从未使用过 no-unused-vars - 11:5 error 'router' is assigned a value but never used no-unused-vars ESLint 抱怨从未使用过本地 state 变量 no-unused-vars - ESLint complains that a local state variable is never used no-unused-vars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM