How to display different navbar component for different reactjs pages

前提是你 提交于 2021-01-29 03:16:00

问题


I am trying to display two different navbar component one as the main site navbar and one for the dashboard navbar.

I have tried creating two reusable components namely:

<LayoutNav /> and <DashboardNav />

Here is their implementation:

const LayoutNav = ({children}) => {
    return (
        <>
            <section className="container-fluid">
                <Navbar />
                {children}
            </section>
        </>
    );
}

it is the same for LayoutDashboardNav i only changed <Navbar /> to <DashboardNav /> below is my route implementation:

<BrowserRouter>
        <Switch>
            <LayoutNav>
                <Route exact path="/registervehicle" component={RegVehicle} />
                <Route exact path="/result" component={SearchResults} />
                <Route exact path="/selectseat" component={PickSeat} />
                <Route exact path="/forgotpassword" component={ForgotPassword} />
                <Route exact path="/resetpassword" component={ResetPassword} />
                <Route exact path="/register" component={Registration} />
                <Route exact path="/login" component={LoginAuth} />
                <Route exact path="/" component={Home} />
            </LayoutNav>

            <LayoutDashboardNav>
                <Route exact path="/companydashboard" component={CompanyDashboard} />
            </LayoutDashboardNav>

            <Route component={NotFound} />
        </Switch>
        <Footer />
    </BrowserRouter>

I expect to see <Navbar /> or <DashboardNav /> only on those pages that are children of the components they are used in. But everything is showing only <Navbar />.


回答1:


You can use a a higher order component to wrap the <Route> component like this. We can have the wrapper component to have some logic to determine which layout to use based on layout prop.

// wrapper component
const DynamicLayoutRoute = props => {
  const { component: RoutedComponent, layout, ...rest } = props;

  // render actual Route from react-router
  const actualRouteComponent = (
    <Route
      {...rest}
      render={props => (
         <RoutedComponent {...props} />
      )}
    />
  );

  // depends on the layout, you can wrap Route component in different layouts
  switch (layout) {
    case 'NAV': {
      return (
        <LayoutNav>
          {actualRouteComponent}
        </LayoutNav>
      )
    }
    case 'DASH_BOARD_NAV': {
      return (
        <LayoutDashboardNav>
          {actualRouteComponent}
        </LayoutDashboardNav>
      )
    }
    default: {
      return (
        <LayoutNav>
          {actualRouteComponent}
        </LayoutNav>
      )
    }
  }
};

Instead of doing the normal <Route exact path="/selectseat" component={PickSeat} />

Now you can do <DynamicLayoutRoute exact path="/selectseat" component={PickSeat} layout="DASH_BOARD_NAV" />



来源:https://stackoverflow.com/questions/57789388/how-to-display-different-navbar-component-for-different-reactjs-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!