Multiple Layout In React Using React Router V4

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.
  1. import React, { Component } from 'react';  
  2. import { Route } from 'react-router-dom';  
  3.   
  4. const LoginLayout = ({ children }) => (                         
  5.     <div>  
  6.       <p>This is the First Layout</p>  
  7.       {children}                                       
  8.     </div>  
  9.   );  
  10.   
  11.   const LoginLayoutRoute = ({component: Component, ...rest}) => {  
  12.     return (  
  13.       <Route {...rest} render={matchProps => (  
  14.         <LoginLayout>  
  15.             <Component {...matchProps} />  
  16.         </LoginLayout>  
  17.       )} />  
  18.     )  
  19.   };  
  20.   
  21. 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. 
  1. <p>This is the First Layout</p>   
Layout 2
 
Create a file named DashboardLayout.js and paste the below code in that file.
  1. import React, { Component } from 'react';  
  2. import { Route } from 'react-router-dom';  
  3.   
  4. const DashboardLayout = ({children, ...rest}) => {  
  5.   return (  
  6.     <div className="page page-dashboard">  
  7.       <div className="sidebar">This is the Second Layout</div>  
  8.       <div className="main">{children}</div>  
  9.     </div>  
  10.   )  
  11. }  
  12.   
  13. const DashboardLayoutRoute = ({component: Component, ...rest}) => {  
  14.   return (  
  15.     <Route {...rest} render={matchProps => (  
  16.       <DashboardLayout>  
  17.           <Component {...matchProps} />  
  18.       </DashboardLayout>  
  19.     )} />  
  20.   )  
  21. };  
  22.   
  23. 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.
  1. <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.
  1. import React, { Component } from 'react';  
  2. const LoginPage = ({  classes }) => {  
  3.     return (  
  4.       <div className="col-md-6 col-md-offset-3">  
  5.                 <h2>Login</h2>  
  6.                 <form name="form">  
  7.                     <div className="form-group" >  
  8.                         <label>Username</label>  
  9.                         <input type="text" className="form-control" />  
  10.                     </div>  
  11.                     <div className="form-group" >  
  12.                         <label>Password</label>  
  13.                         <input type="password" className="form-control" />  
  14.                     </div>                      
  15. <div className="form-group">  
  16.                         <button type="submit" className="btn btn-primary">Login</button>  
  17.                     </div>  
  18.                 </form>  
  19.             </div>  
  20.     );  
  21.   };  
  22.   
  23.   export default LoginPage  
Here, we have created the demo login page which will use the layout 1, i.e., Login Layout.
 
Component 2
 
Create a file named UserPage.js and paste the below code.
  1. import React, { Component } from 'react';  
  2. const UserPage = ({  classes }) => {  
  3.     return (  
  4.       <div>  
  5.         <h2>Welcome User</h2>  
  6.       <span>Welcome the demo of multiple page this page is rendered using second layout</span>  
  7.       </div>  
  8.     );  
  9.   };  
  10.   
  11.   export default UserPage  
Here, we have created the demo Dashboard page which will use layout 2, i.e., Dashboard Layout.
 
Step 3
 
Now, update your App.js file to use both - the layout and the component - as below.
  1. import React, { Component } from 'react';  
  2. import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-dom';  
  3.   
  4. /** Layouts **/  
  5. import LoginLayoutRoute from "./LoginLayout";  
  6. import DashboardLayoutRoute from "./DashboardLayout";  
  7.   
  8. /** Components **/  
  9. import UserPage from './UserPage';  
  10. import LoginPage from './LoginPage'  
  11.   
  12. /* 
  13.    App 
  14.  */  
  15. class App extends Component {  
  16.   render() {  
  17.     return (  
  18.       <Router>  
  19.         <Switch>  
  20.           <Route exact path="/">  
  21.             <Redirect to="/layout1" />  
  22.           </Route>  
  23.           <LoginLayoutRoute path="/layout1" component={LoginPage} />  
  24.           <DashboardRoute path="/layout2" component={UserPage} />  
  25.         </Switch>  
  26.       </Router>  
  27.     );  
  28.   }  
  29. }  
  30.   
  31. export default App;   
Here, we have updated the App.js file as defined in the route. When we route to /layout1, then LoginLayout (our first layout) will be used as the master page for the component Login page. On the other hand, when we route to /layout2, then our second layout, i.e., DashbaordLayout will be used as the master page for the component UserPage.
 
Here, our default layout is "/lauout1".
 
Now, run your app and you’ll be able to see the following output. This login page is using the first layout.
 
Multiple Layout in React with React Router V4 
 
Now, change the route to /layout2 and you will be able to see the following output which is using the second layout.
 
Multiple Layout in React with React Router V4
 

Conclusion

 
As we can see here, by changing the route, different master pages are rendered for different components in DOM.
 
Thanks to My Colleague friend Mr. Ajay Bhaskar to help me with this article.