在React Router中渲染多个组件

Hub*_* OG 7 javascript meteor reactjs react-jsx react-router

我习惯了具有多个屈服区域的应用程序布局,即内容区域和顶栏标题.我想在React Router中实现类似的功能.例如:

<Router>
  <Route path="/" component = { AppLayout }>
    <Route path="list"
           component = { ListView }
           topBarComponent = { ListTopBar }/>
  </Route>
</Router>
Run Code Online (Sandbox Code Playgroud)

AppLayout:

<div className="appLayout box">
  <div className="appLayout topBar">
    { -- display ListTopBar here -- }
  </div>
  <div className="appLayout content">
    { -- display ListView here -- }
  </div>     
</div>
Run Code Online (Sandbox Code Playgroud)

两个子组件都应该接收相同的道具.

我怎么处理这个?

Cre*_*ema 14

要传递多个组件,您可以这样做:

<Route path="groups" components={{main: Groups, sidebar: GroupsSidebar}} />
<Route path="users" components={{main: Users, sidebar: UsersSidebar}}>
Run Code Online (Sandbox Code Playgroud)

请参阅此处的文档:https: //github.com/ReactTraining/react-router/blob/v3/docs/API.md#named-components

  • 链接链接已损坏.它应该是:https://github.com/ReactTraining/react-router/blob/v3/docs/API.md#named-components (2认同)

Mal*_*nda 6

您可以使用 Fragments,而不是使用 div。`

<Route path='/some-path' render={props =>
  <Fragment>
    <Child 1/>
    <Child 2/>
  </Fragment>
} />
Run Code Online (Sandbox Code Playgroud)

`


Fel*_*ger 5

根据文档,在v4中,您可以渲染多个组件,如下所示:

<Route path='/some-path' render={props =>
  <div>
    <FirstChild />
    <SecondChild />
  </div>
} />
Run Code Online (Sandbox Code Playgroud)


小智 5

您还可以在最新版本的 React-router-dom 中使用 Array;

<Route path="groups" element={[<Component1/>,<Component2/>]} />
Run Code Online (Sandbox Code Playgroud)

会工作得很好。