How to implement multiple layouts using React router V4?

How to Use Multiple Layout With React Router v4

There may have multiple layouts when working on massive projects while developing a project. We are frequently forced to divide our programs into pieces, each program with its unique layout. There are numerous occasions when multiple layouts are required for a website.

What is React Router?

React Router is a standard routing library for React.

The most basic example is when we need to separate the administration and client parts of a website, or when we need to change the page layout significantly for an unauthorized user. Moreover, you can hire React developers who specialized in operating and solving complex problems in React.

In this article, We’ll show how to use multiple layouts in a React application without utilizing redundant mountings with React Router v4.

Checkout This Also: React Hooks Best Practices in 2022

Installing React Router Dom:

npm install --save react-router-dom

Initial setup:

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";
import Router from "components/router";

const MyApp = props => (
      <Router />

ReactDOM.render(<MyApp />, document.getElementById("app"));


Step 1:

To introduce multiple layouts or master pages to React, we’ll first construct two distinct layout files and their respective routes.


We’ve constructed a custom component to serve as our first master page or layout, as well as the route for the “LoginPageRoute” layout. For any child components that use this layout, a child component will be rendered at children, with the rest of the elements remaining the same.


import React, { Component } from 'react';  
import { Route } from 'react-router-dom';  
const DashboardLayout = ({children,}) => {  
  return (  
    <div className="page page-dashboard">  
      <div className="main">{children}</div>  
const DashboardLayoutRoute = ({component: Component,}) => {  
  return (  
    <Route {} render={matchProps => (  
          <Component {...matchProps} />  
    )} />  
export default DashboardLayout;  

We’ve made a custom component that will serve as our second master page or layout, as well as a route for the layout “DashboardLayout.”

In this example, the element below will be the same for all Dashboard Layout child components.

Step 2:

In the next stage, we’ll make two components for each layout. The first layout will be used for our first component, while the second layout will be used for the second component.


import React, { Component } from 'react';  
const LoginPageForm = ({  classes }) => {  
    return (  
      <div className="col-md-6 col-md-offset-3">  
                <form name="form">  
                    <div className="form-group" >  
                        <input type="text" className="form-control" />  
                    <div className="form-group" >  
                        <input type="password" className="form-control" />  
<div className="form-group">  
                        <button type="submit" className="btn btn-primary">Login</button>  
  export default LoginPageForm

Schedule an interview with React developers


import React, { Component } from 'react';  
const UserPage = ({  classes }) => {  
    return (  
        <h2>Welcome User</h2>  
  export default UserPage  




Layout 1
Layout 1



Layout 2
Layout 2



As you can see, altering the route renders various master pages for distinct DOM components. With React Router v4, we simply wrap our layout component around the routes to leverage different layouts.

Thank for Reading. Hope you enjoyed our article.

Hire top-notch React experts from Bosc Tech for React development requirements.

Get in touch

    Stay up-to-date with our blogs