0

I am using React-router recently and found it was not that intuitive. Here is my case. I defined a stateless React component "MainLayout" which has two parameters to include "headerBar" component and "children" component.

mainlayout.js:

const MainLayout = ({children,headerBar})=>{
  return (
    <div className="app">
      {headerBar}
      <main>
      {children}
      </main>
    </div>
  );
}
export default MainLayout;

My routes are like this:

routes.js:

export default (
  <Router history={browserHistory}>
    <Route component={MainLayout}> -----> how to specify "headerBar"
      <Route path="/" component={Home} />

      <Route path="widgets">
        <Route component={SearchLayout}>
          <IndexRoute component={WidgetList} />
        </Route>
      </Route>

    </Route>
  </Router>
);

In such a case, how to specify "headerBar" in Route of "MainLayout"?

Thanks

Derek

derek
  • 9,358
  • 11
  • 53
  • 94

1 Answers1

0

With react router, you can declare multiple components in sub routes. This features is called Named Components.

This topic has been treated here too: Using React-Router with a layout page or multiple components per page

You should write somthing like:

<Route path="/" components={{body: Home, header: HeaderBar}} />

and

const MainLayout = ({body,header})=>{
  return (
    <div className="app">
      {header}
      <main>
      {body}
      </main>
    </div>
  );
}

Regards

Community
  • 1
  • 1
Damien Leroux
  • 11,177
  • 7
  • 43
  • 57