[英]React Navbar onClick redirect component
I have doubt about how to redirect another component when i click nevbar items..当我单击 nevbar 项目时,我怀疑如何重定向另一个组件。
Here i have one profile component separated in another folder, if i click profile icon need to go profile component and show that page.在这里,我在另一个文件夹中分离了一个配置文件组件,如果我单击配置文件图标需要转到配置文件组件并显示该页面。 I tried to many way but its not showing properly.
我尝试了很多方法,但显示不正确。
In this picture, if click "Edit profile" need to redirect my profile component.在这张图片中,如果点击“编辑个人资料”需要重定向我的个人资料组件。
Below my example code:在我的示例代码下面:
//Navbar.js
import { useState } from "react";
import myProfile from "../../profile/myProfile";
import { BrowserRouter as Router, NavLink, Redirect, Prompt } from "react-router-dom";
import Route from "react-router-dom/Route";
import { Link } from "react-router-dom";
const renderMenu = (
<Menu
anchorEl={anchorEl}
anchorOrigin={{ vertical: "top", horizontal: "right" }}
id={menuId}
keepMounted
transformOrigin={{ vertical: "top", horizontal: "right" }}
open={isMenuOpen}
onClose={handleMenuClose}>
<div className="profile-bar">
<button onClick={routing}>Edit Profile</button>
</div>
<MenuItem onClick={(e) => authProvider.logout(e)}>Logout</MenuItem>
</Menu>
);
const routing = (
<Router>
<div>
<Route to="/editprofile" component={myProfile} />
</div>
</Router>
);
Replace this,换这个,
<div className="profile-bar">
<Link onClick={routing}>Edit Profile</Link>
</div>
With this,有了这个,
<MenuItem>
<Link to="/editprofile">Edit Profile</Link>
</MenuItem>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.