How To Create A Layout Component: React

Step by Step Guide on creating a layout on a create-react-app Project

ยท

2 min read

How To Create A Layout Component: React

When creating a react application/website, most of the pages would be sharing the same content all over. For example the navigation bar and page footer. Instead of importing each component in every page to be rendered, it is much easier and faster to just create a layout component.

This post will cover how to create a react app using Layout Components.

Live demo: hjpx0v.csb.app

Source Code to the demo: github.com/achingachris/react-layout-demo

Here is how to do it:

Creating a React App using create-react-app

To start a new React application, run the following on the CLI of your developer environment:

npx create-react-app react-layout-demo
cd react-layout-demo
npm start

If the installation is successful, open the browser and go to localhost:3000:

Default react landing page

Creating the Layout Component

Inside the src directory, create a new folder: components/ and inside the new folder, add a new file: Layout.js

Inside the new file, create a new component.

You can choose either class component or functional component that works best for you ๐Ÿ˜„


const Layout = () => {
  return (
    <div>Layout</div>
  )
}

export default Layout

Making the Component a Layout Component

To make the component, a layout component, we will use React inheritance, which allows the contents of the component used anywhere the Layout component will be imported. To do that, we will use the children props:

const Layout = ({ children }) => {
  return <div>{children}</div>
}

export default Layout

Inside the src/components directory, add two new files: Navigation.js and Footer.js and update the content as follows:

Navigation.js

Footer.js

To add the Navigation and Footer on the Layout component, update the Layout.js to:

import Navigation from './Navigation'
import Footer from './Footer'

const Layout = ({ children }) => {
  return (
    <>
      <Navigation />
      <main>{children}</main>
      <Footer />
    </>
  )
}

export default Layout

import NavigationBar from '../components/NavigationBar'
import Footer from '../components/Footer'

const Layout = ({children}) => {
 return (
  <div>
    <NavigationBar />
    <Footer />
  </div>
 )
}

Using the Layout Component

Import the component into the pages needed i.e

import Layout from '../layout/Layout'

const AboutPage = () => {

return (
  <Layout>
    //page content
  </Layout>
)

}

Ensure that all the contents in the component are wrapped inside the <Layout></Layout>

Conclusion

The post has covered the steps to create a Layout component in React.

Codesandbox