Next-js-Boilerplate icon indicating copy to clipboard operation
Next-js-Boilerplate copied to clipboard

Use getLayout function to customize layout

Open riolly opened this issue 3 years ago • 3 comments

As 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} />)
}

riolly avatar Jul 02 '22 04:07 riolly

@riolly, this also something I use personally but I didn't integrate it. Should we?

ixartz avatar Jul 02 '22 22:07 ixartz

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.

riolly avatar Jul 04 '22 02:07 riolly

I think the getLayout pattern should be implemented.

Dannymx avatar Aug 20 '22 22:08 Dannymx

I think we can directly switch to the app directory when the feature come out from the beta and there are still some missing feature: https://beta.nextjs.org/docs/app-directory-roadmap#planned-features

Hope the beta won't last too long.

ixartz avatar Oct 28 '22 21:10 ixartz

Planning to switch directly to app directory when the feature come out from the beta.

ixartz avatar Nov 19 '22 19:11 ixartz