Adding a layout to NextJS - part 3

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.
Now that we have our basic layout let's spend this article on styling the header and footer to resemble the design. As a reminder, the design I picked looks like this. You can see the circled header and footer. Styling the header Let's start with th...
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...

Now that we have our basic application set up, we can dive into the more detailed work.
In part 1 I asked you to think about the global structure you are seeing, and in the template I picked, we got to know there were three designed pages.

The common element on all these three pages is the header and footer part. They look the same on every page and thus can be created in one layout file, so we don't have to repeat ourselves.
In this article, I'll show you how to do just that.
Let's start by adding our layout file. For this, we must first add a components folder to our project.
Then inside, create a file called layout.js.
Let's start with some hardcoded layout elements, and we'll move them to our components later.
export default function Layout({ children }) {
return (
<>
<header>Header</header>
<main>{children}</main>
<footer>© 2022 - Our portfolio</footer>
</>
);
}
As you can see, a layout is technically simply a component that renders some other elements.
Inside, it renders "children", which is whatever we put inside of it.
To use this layout, we need to load it from our _app.js file.
This file is the entry point for our application.
It will return whatever we throw at it by default, but we can wrap that into this layout.
import '../styles/globals.css';
import Layout from '../components/layout';
function MyApp({ Component, pageProps }) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
);
}
export default MyApp;
As you can see, the component element is now wrapped in our layout, so the Component will become the children part as defined there.
You can now run your project to see it in action. The page should now have the header and footer. They don't look much yet, but we'll discuss that in the following article.

If you want to see the code for today, I uploaded it to GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter