[英]How to link React Components inside anchor tag
I have several components and routes in my app.我的应用程序中有几个组件和路由。 I have a sidebar menu that has links to other components in my app.
我有一个侧边栏菜单,其中包含指向我的应用程序中其他组件的链接。 I tried adding the components but it does not ho anything, it renders a blank page.
我尝试添加组件,但它没有任何内容,它呈现一个空白页面。 Not sure what I am doing wrong.
不知道我做错了什么。 Any help appreciated.
任何帮助表示赞赏。
This is my Component:这是我的组件:
import React from 'react';
import AddNewFileMT from './AddNewFileMT';
import Home from '../routes/Home';
const SideMenu = () => {
return (
<div>
<nav>
<div>
<ul className="sidebar">
<br />
<li>
<a href={<Home />}>Home<i className="fas fa-home"></i></a>
</li>
<li>
<a href={<Archive />}>Archive<i className="fas fa-archive"></i></a>
</li>
<li>
<a href={<AddNewFileMT />}>Add New<i className="fas fa-folder-open"></i></a>
</li>
<li>
<a href={<Analytics />}>Analytics<i className="fas fa-chart-pie"></i></a>
</li>
</ul>
</div>
</nav>
</div>
);
};
export default SideMenu;
Then in my Route I have this:然后在我的路线中,我有这个:
import React from 'react'
import Header from '../components/Header'
import SideMenu from '../components/SideMenu'
import RegionalMap from '../components/RegionalMap'
import Footer from '../components/Footer'
export const Home = () => {
return (
<div>
<Header />
<SideMenu />
<RegionalMap/>
<Footer />
</div>
)
};
export default Home;
This is the address in the browser when a sidemenu item is clicked: http://localhost:3001/[object%20Object]这是单击侧菜单项时浏览器中的地址: http://localhost:3001/[object%20Object]
As React documentation , you can use Link element instead anchor tag.作为React 文档,您可以使用 Link 元素代替锚标记。 For example:
例如:
<Link to="/about">About</Link>
Take a look at the official documentation about Link elements看一下关于 Link 元素的官方文档
In your case, you can use在您的情况下,您可以使用
<Link to={ ROUTES.HOME } key='Home'>Home</Link>
You need to install react-router-dom你需要安装react-router-dom
import Router and Route then wrap your pages between a <Router>
tag.导入路由器和路由,然后将您的页面包装在
<Router>
标记之间。 Like this:像这样:
<Router>
<Route component={componentHere} />
...
</Router>
then import Link in your navigation bar wrap your links with <Link>
then add attribute <Link to={/yourComponentName} />
然后在导航栏中导入 Link 用
<Link>
包裹你的链接,然后添加属性<Link to={/yourComponentName} />
Hint: add exact attribute to your homepage otherwise all URL will redirect to "/" which is your homepage.提示:将确切属性添加到您的主页,否则所有 URL 将重定向到您的主页“/”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.