【问题标题】:Using multiple layouts for react-router components为 react-router 组件使用多种布局
【发布时间】:2016-03-03 23:07:56
【问题描述】:

如果我有以下情况:

<Route path="/" component={Containers.App}>

  { /* Routes that use layout 1 */ }
  <IndexRoute component={Containers.Home}/>
  <Route path="about" component={Containers.About}/>
  <Route path="faq" component={Containers.Faq}/>
  <Route path="etc" component={Containers.Etc}/>

  { /* Routes that use layout 2 */ }
  <Route path="products" component={Containers.Products}/>
  <Route path="gallery" component={Containers.Gallery}/>
</Route>

我怎样才能让两组路线各自使用不同的布局。

如果我只有一个布局,那么我会把它放在 App 中,但是在这种情况下,我应该在哪里定义布局?

为了使它更加复杂,一些布局组件(例如顶部导航)在两种布局类型之间共享。

【问题讨论】:

标签: javascript react-router


【解决方案1】:

您可以使用不带路径的路由来定义 url 未定义的容器:

<Route path="/" component={Containers.App}>

  { /* Routes that use layout 1 */ }
  <Route component={Containers.Layout1}>
    <IndexRoute component={Containers.Home}/>
    <Route path="about" component={Containers.About}/>
    <Route path="faq" component={Containers.Faq}/>
    <Route path="etc" component={Containers.Etc}/>
  </Route>

  <Route component={Containers.Layout2}>
    { /* Routes that use layout 2 */ }
    <Route path="products" component={Containers.Products}/>
    <Route path="gallery" component={Containers.Gallery}/>
  </Route>
</Route>

布局组件然后可以导入其他组件,例如顶部导航

【讨论】:

  • 你能用你的 Layout1 和 Layout 2 做一个要点吗?这会很有帮助。谢谢
  • 你只需要在你的渲染函数中调用 {this.props.children} 。这是一个演示组件
  • @SystemicPlural 提到的 github 仓库不再存在,还有其他可用的仓库吗?
  • 这种方法不再有效了吗?所有给定的链接都已失效,我无法使其在我的项目中按预期运行。
  • Soo... 这就是我发现的(使用 Meteor Base 由 The Meteor Chef 仅供参考):我移动了我想要使用与正常布局不同的布局的路线,使其高于我的其他路线在routes.js。在那之后,一切都很完美。出于某种原因,我试图到达的路线给了我一个 404,否则。
【解决方案2】:

Route 的 path 属性已经接受了一段时间的字符串数组。见https://github.com/ReactTraining/react-router/pull/5889/commits/4b79b968389a5bda6141ac83c7118fba9c25ff05

简化以匹配问题路线,但我有多个布局基本上是这样的(使用 react-router 5):

<App>
  <Switch>
    <Route path={["/products", "/gallery"]}>
      <LayoutTwo>
        <Switch>
          <Route path="/products" component={Products} />
          <Route path="/gallery" component={Gallery} />
        </Switch>
      </LayoutTwo>
    </Route>
    {/* Layout 1 is last because it is used for the root "/" and will be greedy */}
    <Route path={["/about", "/faq", "/etc", "/"]}>
      <LayoutOne>
        <Switch>
          <IndexRoute component={Home} />
          <Route path="/about" component={About} />
          <Route path="/faq" component={Faq} />
          <Route path="/etc" component={Etc} />
        </Switch>
      </LayoutOne>
    </Route>
  </Switch>
</App>

此解决方案可防止在路线更改时重新安装布局,这可能会中断转换等。

【讨论】:

  • 这适用于我的反应路由器 v5。
【解决方案3】:

您可以创建一个函数 RouteWithLayout 来呈现包装在布局中的 &lt;Route&gt;:

const RouteWithLayout = ({ component: Component, layout: Layout, ...rest }) => (
  <Route {...rest} render={props => (
    <Layout>
      <Component {...props} />
    </Layout>
  )} />
)

const MainLayout = props => (
  <div>
    <h1>Main</h1>
    {props.children}
  </div>
)

const AltLayout = props => (
  <div>
    <h1>Alt</h1>
    {props.children}
  </div>
)

const Foo = () => (
  <p>Foo</p>
)

const Bar = () => (
  <p>Bar</p>
)

const App = () => (
  <div>
    <Switch>
      <RouteWithLayout exact path="/foo" layout={MainLayout} component={Foo} />
      <RouteWithLayout exact path="/bar" layout={AltLayout} component={Bar} />
    </Switch>
  </div>
)

【讨论】:

  • 对于我的用例,这是最干净的方法。谢谢!
【解决方案4】:

我遇到了这个问题,并找到了我想分享的解决方案。

使用 react router v4,我们可以直接在您的布局中渲染路由。哪个更具可读性和易于维护。

布局

export class MainLayout extends React.PureComponent {
  render() {
    return (
      <div>
        <Header />
        {this.props.children}
        <Footer />
      </div>
    );
  }
}

Mainlayout.propTypes = {
  children: PropTypes.node.isRequired,
}

路由器

export default function App() {
  return (
    <Switch>
      <MainLayout>
        <Switch>
          <Route path="/" component={Home} />
          <Route path="/about" component={About} />
        </Switch>
      </MainLayout>
      <OtherLayout>
        .... other paths
      </OtherLayout>
    </Switch>
  );
}

【讨论】:

  • 使用 2 个开关有效!但是什么时候使用两个开关呢?
【解决方案5】:

这是使用具有不同 React 组件的多个布局的好方法。

在您的路由器中,您可以使用:

<Router history={browserHistory}>
  <Route component={MainLayout}>
    <Route path="/" component={Home} />
    <Route path="/about" component={About} />
  </Route>
  <Route component={EmptyLayout}>
    <Route path="/sign-in" component={SignIn} />
  </Route>
  <Route path="*" component={NotFound}/>
</Router>

来源:https://sergiotapia.me/different-layouts-with-react-router-71c553dbe01d

【讨论】:

  • 这个和decorators基本一样
  • 如果MainLayout 有header 和footer 并且您想将您的路线放在它们之间怎么办?
  • @vsync 然后确保在header 和MainLayout 组件中的footer 之间渲染{this.props.children}。
【解决方案6】:

Pintouch,我可以通过以下示例来实现这一点:

布局1:

import React from 'react'

const Layout1 = (props) => (
    <div>
        <h1>Layout 1</h1>
        {props.children}
    </div>
)

export default Layout1

布局2:

import React from 'react'

const Layout2 = (props) => (
    <div>
        <h1>Layout 2</h1>
        {props.children}
    </div>
)

export default Layout2

布局容器:

import React from 'react'

const LayoutContainer = (props) => (
    <div>
                {props.children}
    </div>
)

export default LayoutContainer

路线:

import React from 'react';
import { Router, Route, IndexRoute, hashHistory } from 'react-router';

import LayoutContainer from './LayoutContainer'
import Layout1 from './Layout1'
import Layout2 from './Layout2'
import ContactManagerView from './ContactManagerView'
import CallerIdView from './CallerIdView'
import NotFound from './NotFound'

<Router history={hashHistory}>
    <Route path="/" component={LayoutContainer}>
        <Route component={Layout1}>
            <IndexRoute component={DashboardView}/>
            <Route path='Contacts' component={ContactManagerView}/>
        </Route>

        <Route component={Layout2}>
            <Route path='CallerId' component={CallerIdView}/>
        </Route>

        <Route component={Layout}>
            <Route path='*' component={NotFound}/>
        </Route>
    </Route>
</Router>

【讨论】:

  • A+ 表示完整性。
  • 好像 React 不喜欢这样:Warning: You should not use &lt;Route component&gt; and &lt;Route children&gt; in the same route; &lt;Route children&gt; will be ignored - solution in this post
猜你喜欢
  • 1970-01-01
  • 2017-08-09
  • 2020-08-29
  • 2016-01-08
  • 2022-07-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2019-01-02
相关资源
最近更新 更多