2

我有三个不同的导航栏。它们中的每一个都是不同的组件。

  • <PublicNavbar />,用于公共页面,例如 LandingPage、DiscoverPage 等。
  • <AccessNavbar />,用于SignInPage、SignUppage、VerificationPage等。
  • <PrivateNavbar />,用于私人页面,例如 NewCampaignPage 等。

我怎样才能正确地展示它们?如果用户已登录,我想将 DiscoverPage 替换PublicNavbarPrivateNavbar.

import React from "react";
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
import "semantic-ui-css/semantic.min.css";
import "./App.css";
import PublicNavbar from "./components/Navbar/PublicNavbar";
import AccessNavbar from "./components/Navbar/AccessNavbar";
import PrivateNavbar from "./components/Navbar/PrivateNavbar";
import Footer from "./components/Footer";
import LandingPage from "./pages/LandingPage";
import SignIn from "./pages/SIgnInPage";
import DiscoverPage from "./pages/DiscoverPage";
import SignUp from "./pages/SignUpPage";
import Verification from "./pages/VerificationPage";
import Registration from "./pages/RegistrationPage";
import RegistrationComplete from "./pages/RegistrationCompletePage";
import NewCampaign from "./pages/NewCampaignPage";

function App() {
  return (
    <Router>
      <div>
        <div id="container">
          <div id="main">
            <Switch>
              <Route path="/" exact component={LandingPage} />
              <Route path="/discover" component={DiscoverPage} />
              <Route path="/signIn" component={SignIn} />
              <Route path="/signUp" component={SignUp} />
              <Route path="/verification" component={Verification} />
              <Route path="/registration" component={Registration} />
              <Route
                path="/registration-complete"
                component={RegistrationComplete}
              />
              <Route path="/new-campaign" component={NewCampaign} />
            </Switch>
          </div>
        </div>
        <Footer />
      </div>
    </Router>
  );
}
export default App;

示例导航栏代码:

import React from "react";

const PublicNavbar = () => {
  return (
    <nav className="navbar navbar-expand-lg bg-white">
      <div className="container-fluid navbar-container">
        <a className="navbar-brand abs nav-bar-title" href="#">
          AshoDaanKori
        </a>
        <button
          className="navbar-toggler ms-auto custom-toggler"
          type="button"
          data-bs-toggle="collapse"
          data-bs-target="#collapseNavbar"
        >
          <span className="navbar-toggler-icon"></span>
        </button>
        <div className="navbar-collapse collapse" id="collapseNavbar">
          <ul className="navbar-nav ms-auto">
            <li className="nav-item active">
              <a
                className="nav-link"
                href=""
                data-bs-target="#myModal"
                data-bs-toggle="modal"
              >
                About
              </a>
            </li>
            <li className="nav-item active">
              <a
                className="nav-link"
                href=""
                data-bs-target="#myModal"
                data-bs-toggle="modal"
              >
                How it works
              </a>
            </li>
            <li className="nav-item active">
              <a
                className="nav-link"
                href=""
                data-bs-target="#myModal"
                data-bs-toggle="modal"
              >
                Discover
              </a>
            </li>
          </ul>
          <a
            className="nav-link navbar-btn"
            href=""
            data-bs-target="#myModal"
            data-bs-toggle="modal"
          >
            Start Campaign
          </a>
        </div>
      </div>
    </nav>
  );
};
export default PublicNavbar;

4

2 回答 2

0

通过条件渲染解决了它。在没有创建多个导航栏组件的情况下,我创建了一个并根据条件更改了内部元素。

const Navbar = ({ isAuthenticated }) => {
  return (
    <nav
      className={`autohide navbar navbar-expand-lg bg-white ${
        isAuthenticated ? "private-navbar" : ""
      }`}
    >
      <div className="container-fluid navbar-container">
        <NavHashLink className="navbar-brand abs nav-bar-title" to="/">
          AshoDaanKori
        </NavHashLink>
        <button
          className="navbar-toggler ms-auto custom-toggler"
          type="button"
          data-bs-toggle="collapse"
          data-bs-target="#main_nav"
          aria-expanded="false"
          aria-label="Toggle navigation"
        >
          <span className="navbar-toggler-icon"></span>
        </button>

        {isAuthenticated ? (
          <div className="collapse navbar-collapse" id="main_nav">
            <ul className="navbar-nav ms-auto">
              <li className="nav-item active">
                <NavHashLink to="/my-fundraisers" className="nav-link">
                  My fundraisers
                </NavHashLink>
              </li>
            </ul>
            <Link to="/start-campaign" className="nav-link navbar-btn">
              Start a new campaign
            </Link>

            {/* User profile */}
            <div className="btn-group nav-item">
              <button
                type="button"
                className="btn "
                data-bs-toggle="dropdown"
                data-bs-display="static"
                aria-expanded="false"
                style={{ padding: "0" }}
              >
                <i aria-hidden="true" className="user circle huge icon"></i>
              </button>
              <ul className="dropdown-menu dropdown-menu-end">
                <li>
                  <h3
                    style={{
                      textAlign: "center",
                      fontSize: 18,
                      fontWeight: 600,
                      color: "#6E6E6E",
                    }}
                  >
                    {userInfo.fullName || "User"}
                  </h3>
                </li>
                <li>
                  <hr className="dropdown-divider" />{" "}
                </li>
                <li>
                  <Link className="dropdown-item" to="/account">
                    Account Settings
                  </Link>
                </li>
                <li>
                  <Link
                    className="dropdown-item"
                    to="#"
                  >
                    Sign Out
                  </Link>
                </li>
              </ul>
            </div>
          </div> //If authenticated, rendered PrivateNavbar elements
        ) : (
          <div className="collapse navbar-collapse" id="main_nav">
            <ul className="navbar-nav ms-auto">
              <li className="nav-item active">
                <NavHashLink
                  smooth
                  to="/#our-story-section"
                  className="nav-link"
                >
                  About
                </NavHashLink>
              </li>
              <li className="nav-item active">
                <NavHashLink to="/how-it-works" className="nav-link">
                  How it works
                </NavHashLink>
              </li>
              <li className="nav-item active">
                <NavHashLink to="/discover" className="nav-link">
                  Discover
                </NavHashLink>
              </li>
            </ul>
            <NavHashLink to="/sign-in" className="nav-link navbar-btn">
              START CAMPAIGN
            </NavHashLink>
          </div> //If not authenticated, rendered PublicNavbar elements
        )}
      </div>
    </nav>
  );
};
export default Navbar;
于 2021-05-07T11:51:26.350 回答
0

创建一个名为 NavBarController 的新导航栏组件。在调用导航栏控制器组件时,将“类型”作为道具传递。类型应该是一个状态并且应该根据用户状态而改变。

<NavBarController type={1}></NavBarController>

让 NavBarController 处理您想要显示的三个导航栏(其中类型为 1,2 或 3)中的任何一个导航栏。

您的 NavBarController 将返回如下内容:

return (props.type===1?<PublicNavbar/>
:props.type===2?<AccessNavbar/>
:props.type===3?<PrivateNavbar/>)
于 2021-05-02T17:22:49.430 回答