如何在不同页面上显示不同的Navbars,React Router版本6?
因此,我的网站将对帐户类型的学生,老师和管理员有三种不同风格的视图(它是一个虚拟的电子学习平台)。我想为学生提供另一种样式的Navbar(登录和登录的两个不同版本),然后是一个不同的老师(又是一个用于登录和登录),然后是另一个用于管理员(单件登录) 。
这就是我现在正在做的。我制作了一个扭曲所有内容的布局组件。
const Layout: FC<LayoutProps> = (props) => {
return (
<div className="flex flex-col min-h-screen dark:bg-gray-800">
<MainNavigationBar />
<main className="mx-auto flex-grow" style={{ width: "90%" }}>
{props.children}
</main>
<Footer />
</div>
);
};
export default Layout;
那就是我的app.tsx的样子。
export default function App() {
return (
<Layout>
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/signup" element={<SignUpPage />} />
<Route path="/login" element={<LogInPage />} />
<Route path="/cart" element={<EmptyCart />} />
<Route path="/cart/:studentId" element={<ShoppingCart />} />
<Route path="/checkout/:studentId" element={<CheckOutPage />} />
</Routes>
</Layout>
);
}
如您所见,尚无教师和管理员的路线,因为IDK最好的是实现每个帐户类型的功能,而每个帐户类型都具有不同的NAVBAR本质上意味着完全不同的视图样式。
我想到了两种方式,但可以帮助我了解哪一种更好。
- 我将Navbar直接放入每个页面组件中。但这意味着每个页面上都会有nav栏的副本的帐户类型,然后更新更改将很难,效率低下。
- 我将所有三个导航栏都放在app.tsx文件中,然后根据哪种帐户类型的活动有条件地渲染导航栏。听起来更好,因为每个帐户类型页面都只有一个导航栏副本。但是问题是,当访问“/老师”或“/admin”时,IDK如何触发/设置“帐户类型活动”。
帮助我了解最佳实践或技术,这是我的第一个大项目,谢谢。
So my website is gonna have three differently styled views for account types student, teacher and admin (its a dummy e-learning platform). I want to have a different styled navbar for student (two different versions for logged in and logged out) then a different one for teacher (again one for logged in and logged out) and then another one for admin(single one for logged in).
This is what Im doing right now. I made a layout component which warps everything.
const Layout: FC<LayoutProps> = (props) => {
return (
<div className="flex flex-col min-h-screen dark:bg-gray-800">
<MainNavigationBar />
<main className="mx-auto flex-grow" style={{ width: "90%" }}>
{props.children}
</main>
<Footer />
</div>
);
};
export default Layout;
Then this is what my App.tsx looks like.
export default function App() {
return (
<Layout>
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/signup" element={<SignUpPage />} />
<Route path="/login" element={<LogInPage />} />
<Route path="/cart" element={<EmptyCart />} />
<Route path="/cart/:studentId" element={<ShoppingCart />} />
<Route path="/checkout/:studentId" element={<CheckOutPage />} />
</Routes>
</Layout>
);
}
As you can see there are no routes for the teacher and admin yet because idk what is the best was to implement the functionality where each account type has different Navbar essentially meaning a totally different view style.
I have thought of two ways but help me understand which one is better.
- I put navbar directly inside each pages component. but that would mean that there will be copies of nav bar on each page for the account types and then updating changes would be hard, feels inefficent.
- I put all three nav bars in the App.tsx file and then conditionally render nav bar depending on which account type is active. this sounds better cause there will only be one nav bar copy per all account type pages. but problem with this is that idk how to trigger/set "account type active" when "/teacher" or "/admin" is visited.
Help me understand the best practice or technique, this is my first big project, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该根据登录的用户类型有条件地渲染标头。根据路线,我猜您的应用程序将具有某种用户身份验证。您可以使用上下文API或诸如Redux之类的库具有全局状态,其中您可以保存已登录的用户的帐户数据(其中包括帐户类型)。然后,您可以有条件地渲染与帐户类型相对应的Navbar。
您可以了解有关上下文在这里。
You should conditionally render the header based on the type of user that is logged in. Based on the routes, I'm guessing your app will have some sort of user auth. You could have a global state using either Context API or a library like Redux, in which you'd save your logged-in users' account data (which includes the account type). You could then conditionally render a navbar that corresponds to the account type.
You can learn more about context here, and Redux here.
我的场景相同,我通过获取
用户
的类型来解决它:我正在与您分享我的代码:
您可以从上面的
code
中获得帮助。I was having the same scenario and I solved it by fetching the type of
user
:I am sharing you my code :
You can take help from my above
code
.