如何在不同页面上显示不同的Navbars,React Router版本6?

发布于 2025-02-11 17:24:13 字数 1526 浏览 0 评论 0原文

因此,我的网站将对帐户类型的学生,老师和管理员有三种不同风格的视图(它是一个虚拟的电子学习平台)。我想为学生提供另一种样式的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本质上意味着完全不同的视图样式。

我想到了两种方式,但可以帮助我了解哪一种更好。

  1. 我将Navbar直接放入每个页面组件中。但这意味着每个页面上都会有nav栏的副本的帐户类型,然后更新更改将很难,效率低下。
  2. 我将所有三个导航栏都放在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.

  1. 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.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2025-02-18 17:24:13

您应该根据登录的用户类型有条件地渲染标头。根据路线,我猜您的应用程序将具有某种用户身份验证。您可以使用上下文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.

木緿 2025-02-18 17:24:13

我的场景相同,我通过获取用户的类型来解决它:

  const { user } = props;
  switch (user?.role) {
    case "STUDENT":
      return <Student />;
    case "TEACHER":
      return <Teacher />;
    case "ADMIN":
      return <Admin />;
    default:
      return <div className="">User not valid.</div>;
  }

我正在与您分享我的代码:

import React from "react";
import { Switch, Route } from "react-router-dom";
import Profile from "../pages/Profile";
import Dashboard from "../pages/Dashboard";
import {Student, Teacher, Admin} from "./User";
import { useSelector } from "react-redux";

const Content = (props) => {
  const { user } = props;
  if (!user) {
    return <div className="">User not valid.</div>;
  }

  switch (user?.role) {
    case "STUDENT":
      return <Student />;
    case "TEACHER":
      return <Teacher />;
    case "ADMIN":
      return <Admin />;
    default:
      return <div className="">User not valid.</div>;
  }
};

export default function Routes() {
  const user = useSelector((s) => s.auth.user);

  return (
    <Switch>
      <Route exact path={"/profile"} component={Profile} />
      <Route exact path={"/dashboard"} component={Dashboard} />
      <Content user={user}/>
    </Switch>
  );
}

您可以从上面的code中获得帮助。

I was having the same scenario and I solved it by fetching the type of user :

  const { user } = props;
  switch (user?.role) {
    case "STUDENT":
      return <Student />;
    case "TEACHER":
      return <Teacher />;
    case "ADMIN":
      return <Admin />;
    default:
      return <div className="">User not valid.</div>;
  }

I am sharing you my code :

import React from "react";
import { Switch, Route } from "react-router-dom";
import Profile from "../pages/Profile";
import Dashboard from "../pages/Dashboard";
import {Student, Teacher, Admin} from "./User";
import { useSelector } from "react-redux";

const Content = (props) => {
  const { user } = props;
  if (!user) {
    return <div className="">User not valid.</div>;
  }

  switch (user?.role) {
    case "STUDENT":
      return <Student />;
    case "TEACHER":
      return <Teacher />;
    case "ADMIN":
      return <Admin />;
    default:
      return <div className="">User not valid.</div>;
  }
};

export default function Routes() {
  const user = useSelector((s) => s.auth.user);

  return (
    <Switch>
      <Route exact path={"/profile"} component={Profile} />
      <Route exact path={"/dashboard"} component={Dashboard} />
      <Content user={user}/>
    </Switch>
  );
}

You can take help from my above code .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文