Use getLayout function to customize layout
See original GitHub issueAs it’s written on the official docs.
// pages/index.js
import Layout from '../components/layout'
import NestedLayout from '../components/nested-layout'
export default function Page() {
return {
/** Your content */
}
}
Page.getLayout = function getLayout(page) {
return (
<Layout>
<NestedLayout>{page}</NestedLayout>
</Layout>
)
}
// pages/_app.js
export default function MyApp({ Component, pageProps }) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout || ((page) => page)
return getLayout(<Component {...pageProps} />)
}
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Basic Features: Layouts - Next.js
When using TypeScript, you must first create a new type for your pages which includes a getLayout function. Then, you must create a...
Read more >Persistent Layout Patterns in Next.js - Adam Wathan
Option 1: Use a single shared layout in a custom `<App>` component ... Option 4: Add a `getLayout` function to your page components....
Read more >5 - getLayout function on page and layout components
5 - getLayout function on page and layout components. 16. Embed Fork Create Sandbox Sign in. Sandbox Info. 5 - getLayout function on...
Read more >Next.js #7 - Custom Layouts - YouTube
Support the Channel:Become a Member: https://www.youtube.com/ansonthedeveloper/joinBecome a Patreon: http://patreon.com/stuyyBuy me a ...
Read more >How to wrap components that use the getLayout() method
This works, but the layout persistence simply doesn't work. It removes all benefits from the first example. Is it possible to fix this?...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think the
getLayout
pattern should be implemented.We use that pattern to avoid rerendering the layout component when navigating to a page with the same layout. (mimicking client-side navigation) And I think those who use this template have been familiar with next.js and (the other tech stack inside it) and will end up in that situation also.