Creating a reusable layout in Next.js

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Yesterday we added a menu to our Next.js application. However, this was hardcoded on one page.
Today we'll take a look at how we can introduce a layout component to have a shared layout for our menu on each page.
Create a file called layout.js in the components folder.
This file will act as the main layout. In our case, it will render the header and the children for each page.
import Header from './Header';
export default function Layout({children, menu}) {
return (
<>
<Header menuItems={menu} />
<main>{children}</main>
</>
);
}
The children are passed through our main page, the _app.js, and we will pass the menu variable, so we'll have a look at how that works next.
Before we go there, let's see how we can retrieve the menu items in one place instead of doing it per page.
For this, we need to open the _app.js file. Here we can add a function called getInitialProps.
This function can be used to retrieve the menu and eventually pass it to our layout.
import {getPrimaryMenu} from '../lib/api';
MyApp.getInitialProps = async () => {
const menuItems = await getPrimaryMenu();
return {menuItems};
};
Let's see how we can now make sure the layout is used everywhere and pass the menu items.
Let's first add the layout to our _app.js file.
import Layout from '../components/Layout';
function MyApp({Component, pageProps}) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
);
}
To access the menu items, we have to add them to the MyApp function.
function MyApp({Component, pageProps, menuItems}) {
return (
<Layout menu={menuItems}>
<Component {...pageProps} />
</Layout>
);
}
And with this, we have a fully functional universal layout.
_app loads the menu and passes it to our layout componentlayout works as the main layout and renders the headerIf we now run our application, we can see the menu work on all pages automatically.

You can find the complete code on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter