Layouts and templates#
Consistency in UI across different pages is crucial for a seamless user experience. Next.js facilitates this through layouts and templates, allowing you to share common UI elements like headers, footers, or sidebars across multiple routes. Next.js provides us with special files layout.js
and template.js
for this.
Layouts#
A layout in Next.js is a special component that wraps the pages and preserves the state between navigations, avoiding unnecessary re-renders and is shared between multiple routes.
A layout can be defined by exporting a React component as default from a file, e.g., layout.js
. This component accepts a prop, children
, which is populated with the child layout or the page when it is rendered.
Here is an example of a layout: