【问题标题】:How to display different navbar component for different reactjs pages如何为不同的 reactjs 页面显示不同的导航栏组件
【发布时间】:2019-09-04 13:24:47
【问题描述】:

我正在尝试显示两个不同的导航栏组件,一个作为主站点导航栏,一个作为仪表板导航栏。

我尝试过创建两个可重用的组件,即:

<LayoutNav /><DashboardNav />

这是他们的实现:

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

LayoutDashboardNav 也一样,我只是将&lt;Navbar /&gt; 更改为&lt;DashboardNav /&gt; 下面是我的路由实现:

<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>

我希望看到&lt;Navbar /&gt;&lt;DashboardNav /&gt; 仅在它们所用组件的子页面上。但所有内容都只显示&lt;Navbar /&gt;

【问题讨论】:

    标签: reactjs react-router react-router-component


    【解决方案1】:

    您可以像这样使用更高阶的组件来包装&lt;Route&gt; 组件。我们可以让包装组件有一些逻辑来根据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>
          )
        }
      }
    };
    

    而不是做正常的 &lt;Route exact path="/selectseat" component={PickSeat} /&gt;

    现在你可以做 &lt;DynamicLayoutRoute exact path="/selectseat" component={PickSeat} layout="DASH_BOARD_NAV" /&gt;

    【讨论】:

    • 我无法完成这项工作 - 当我尝试实施时,它说“未定义 userContext” - 在这种情况下,带有 userContext 的平面是什么?
    • @implum 我更新了答案。 “userContext”部分根本不应该存在。很抱歉造成混乱。
    • 我还在苦苦挣扎-它只是说'无法读取未定义的'props'的属性,然后引用此: const DynamicLayoutRoute = props => { const { component: RoutedComponent, layout, ...rest } = 这个.props; // 从 react-router 渲染实际路由 const actualRouteComponent = (
    • 如果你能提供帮助,请看这里:stackoverflow.com/questions/62871525/…
    猜你喜欢
    • 2021-07-25
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2022-01-24
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多