Skip to content

Instantly share code, notes, and snippets.

@avinmathew
Created August 8, 2017 11:54
Show Gist options
  • Star 90 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save avinmathew/e82fe7e757b20cb337d5219e0ab8dc2c to your computer and use it in GitHub Desktop.
Save avinmathew/e82fe7e757b20cb337d5219e0ab8dc2c to your computer and use it in GitHub Desktop.
Multiple layouts with React Router v4
import React from "react"
import { Route, Switch } from "react-router-dom"
const AppRoute = ({ 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>
<AppRoute exact path="/foo" layout={MainLayout} component={Foo} />
<AppRoute exact path="/bar" layout={AltLayout} component={Bar} />
</Switch>
</div>
)
@cairocoder
Copy link

That's very useful, thanks a lot.

@cyonder
Copy link

cyonder commented Oct 20, 2017

It looks too good to be true. I tried and it's working. I created a layouts folder in my components and added AppLayout and HomeLayout components.

const Root = ({ store }) => {
    return(
        <Provider store={ store }>
            <BrowserRouter>
                <div>
                    <Switch>
                        <AppRoute exact path="/" layout={ HomeLayout } component={ Home } />
                        <AppRoute path="/signin" layout={ HomeLayout } component={ Signin } />
                        <AppRoute path="/signup" layout={ HomeLayout } component={ Signup } />
                        <AppRoute path="/signout" layout={ HomeLayout } component={ Signout } />
                        <AppRoute path="/dashboard" layout={ AppLayout } component={ RequireAuthentication(Dashboard) } />
                    </Switch>
                </div>
            </BrowserRouter>
        </Provider>
    )
};

Thanks a lot.

@suazithustra
Copy link

thanks yall

@Olegborodko
Copy link

I was looking for this 2 hours .. Thank You )

@simon1400
Copy link

Coool!!
Thanks!!

@anthonator
Copy link

It seems like once a React app hits moderate complexity React Router v4 starts to look similar to v3. React Router v4's sweet spot seems to be single layout apps. Once you introduce multiple layouts the App component turns into a routes file. Maybe I'm just misunderstanding but sometimes I think the premise of v4 is flawed.

@bilal68
Copy link

bilal68 commented Jul 30, 2018

[eslint] 'component' is missing in props validation (react/prop-types)

this error is showing

@johnakhilomen
Copy link

Thanks alot!

@richellyitalo
Copy link

I implemented a default layout this way:

const AppRoute = ({ component: Component, layout: Layout, ...rest }) => (
  if (Layout === undefined) {
    Layout = props => (<React.Fragment>{props.children}</React.Fragment>) // or you can set 'Layout = DefaultLayout'
  }
  <Route {...rest} render={props => (
    <Layout>
      <Component {...props} />
    </Layout>
  )} />
)

@eldrego
Copy link

eldrego commented Dec 13, 2018

Thanks a lot. Was actually what I needed.

@masoudtahmasebi93
Copy link

Thanks , it really solved my problem

@halfbug
Copy link

halfbug commented May 24, 2019

RequireAuthentication

would you share the code of RequireAuthentication here

@dersonsena
Copy link

I'm using the React Router 4 and my is return:

Warning: You should not use <Route render> and <Route children> in the same route; <Route render> will be ignored

Somebody help me

@thomas-avada
Copy link

Save my day today!

@devrsingh2
Copy link

Thanks a lot :)
it saves a lot of time for me.

@aviadhadidainx
Copy link

The problem here that the layout is recreating itself every route.. Someone has a solution for that?
Beside performance it also losses the context of the layout(The chosen menu item and stuff like that

@kaotypr
Copy link

kaotypr commented Dec 2, 2019

im using react-router location.state to determine each page layout.
you can check here https://github.com/adhityasan/kao-react-template/blob/master/src/routes/index.jsx

@micessien
Copy link

Very usefull thanks

@enero-o
Copy link

enero-o commented Apr 26, 2020

This would cause unnecessary rerendering in every client side navigation

@mlbd-tanvir-raj
Copy link

This would cause unnecessary rerendering in every client side navigation

@aiveneh do you have any idea how we can prevent rerendering

@vipinc007
Copy link

This was really useful to me. it saved my day

@damoqiongqiu
Copy link

Thank you, this is cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment