Introduction
In this article, we will learn how to implement multiple layouts in React using React Router v4. Here, I will show you how we can create multiple layouts or master pages and use those layouts for different components using the React Router. And based on the route, these different layouts will be rendered in our React DOM.
What is React?
React is an open-source JavaScript library for building user interfaces of single-page applications. React is managed by Facebook.
What is React Router?
React Router is a standard routing library for React.
Step 1
In the first step, we will create two different layout files and their respective routes to implement multiple layouts or master pages to React.
Layout 1
Create a file named LoginLayout.js and paste the below code in the file.
- import React, { Component } from 'react';
- import { Route } from 'react-router-dom';
- const LoginLayout = ({ children }) => (
- <div>
- <p>This is the First Layout</p>
- {children}
- </div>
- );
- const LoginLayoutRoute = ({component: Component, ...rest}) => {
- return (
- <Route {...rest} render={matchProps => (
- <LoginLayout>
- <Component {...matchProps} />
- </LoginLayout>
- )} />
- )
- };
- export default LoginLayoutRoute;
Here, we have created a custom component that will be used as our first master page or layout, and also, we have created the route for the “LoginLayoutRoute” layout. A child component will be rendered at {children} and the rest of the elements will remain the same for all the child components that are using this layout.
In this case, the element below will be the same for all child components using Login Layout.
- <p>This is the First Layout</p>
Layout 2
Create a file named DashboardLayout.js and paste the below code in that file.
- import React, { Component } from 'react';
- import { Route } from 'react-router-dom';
- const DashboardLayout = ({children, ...rest}) => {
- return (
- <div className="page page-dashboard">
- <div className="sidebar">This is the Second Layout</div>
- <div className="main">{children}</div>
- </div>
- )
- }
- const DashboardLayoutRoute = ({component: Component, ...rest}) => {
- return (
- <Route {...rest} render={matchProps => (
- <DashboardLayout>
- <Component {...matchProps} />
- </DashboardLayout>
- )} />
- )
- };
- export default DashboardLayoutRoute;
Here, we have created a custom component that will be used as our second master page or layout, and also, we have created a route for layout “DashboardLayoutRoute”.
In this case, the element below will be the same for all child components using Dashboard Layout.
- <div className="sidebar">This is the Second Layout</div>
Step 2
In the second step, we will create two components for both layouts respectively. Our first component will use the first layout and the second one will use the second layout.
Component 1
Create a file named LoginPage.js and paste the below code.


ram pukarPosted Dec 17, 2020, 10:50 PM
I am getting error on console -DashboardLayoutRoute is not defined
Rakesh RankawatPosted Jun 2, 2020, 7:56 AM
You saved my ass you know, love you man!!!
Oli SteadmanPosted Sep 12, 2019, 3:19 AM
Thanks this was a super helpful resource.
jino cjPosted Jul 3, 2019, 3:01 AM
If want to have multiple routes in a single layout for example if we also need signup page and want to have route as' layout1/signup'. can we pass a json of routes to the layout1 ?
MrKilofafeure MrKilofafeurePosted Jun 27, 2019, 8:06 AM
Hi, I'm newbe with React and I've been following your example that is great, but once I'm logged and the second layout is loaded I'm not able to load another component inside this second layout. How could achieve it? I'm trying it with a Link react-router-dom. Thanks in advance. Best regards.