Layouts

Examples

Note: We are introducing improved layouts support in Next.js. Read the Layouts RFC for more details and to provide feedback.

The React model allows us to deconstruct a page into a series of components. Many of these components are often reused between pages. For example, you might have the same navigation bar and footer on every page.

  1. // components/layout.js
  2. import Navbar from './navbar'
  3. import Footer from './footer'
  4. export default function Layout({ children }) {
  5. return (
  6. <>
  7. <Navbar />
  8. <main>{children}</main>
  9. <Footer />
  10. </>
  11. )
  12. }

Examples

Single Shared Layout with Custom App

If you only have one layout for your entire application, you can create a Custom App and wrap your application with the layout. Since the <Layout /> component is re-used when changing pages, its component state will be preserved (e.g. input values).

  1. // pages/_app.js
  2. import Layout from '../components/layout'
  3. export default function MyApp({ Component, pageProps }) {
  4. return (
  5. <Layout>
  6. <Component {...pageProps} />
  7. </Layout>
  8. )
  9. }

Per-Page Layouts

If you need multiple layouts, you can add a property getLayout to your page, allowing you to return a React component for the layout. This allows you to define the layout on a per-page basis. Since we’re returning a function, we can have complex nested layouts if desired.

  1. // pages/index.js
  2. import Layout from '../components/layout'
  3. import NestedLayout from '../components/nested-layout'
  4. export default function Page() {
  5. return {
  6. /** Your content */
  7. }
  8. }
  9. Page.getLayout = function getLayout(page) {
  10. return (
  11. <Layout>
  12. <NestedLayout>{page}</NestedLayout>
  13. </Layout>
  14. )
  15. }
  1. // pages/_app.js
  2. export default function MyApp({ Component, pageProps }) {
  3. // Use the layout defined at the page level, if available
  4. const getLayout = Component.getLayout || ((page) => page)
  5. return getLayout(<Component {...pageProps} />)
  6. }

When navigating between pages, we want to persist page state (input values, scroll position, etc.) for a Single-Page Application (SPA) experience.

This layout pattern enables state persistence because the React component tree is maintained between page transitions. With the component tree, React can understand which elements have changed to preserve state.

Note: This process is called reconciliation, which is how React understands which elements have changed.

With TypeScript

When using TypeScript, you must first create a new type for your pages which includes a getLayout function. Then, you must create a new type for your AppProps which overrides the Component property to use the previously created type.

  1. // pages/index.tsx
  2. import type { ReactElement } from 'react'
  3. import Layout from '../components/layout'
  4. import NestedLayout from '../components/nested-layout'
  5. import type { NextPageWithLayout } from './_app'
  6. const Page: NextPageWithLayout = () => {
  7. return <p>hello world</p>
  8. }
  9. Page.getLayout = function getLayout(page: ReactElement) {
  10. return (
  11. <Layout>
  12. <NestedLayout>{page}</NestedLayout>
  13. </Layout>
  14. )
  15. }
  16. export default Page
  1. // pages/_app.tsx
  2. import type { ReactElement, ReactNode } from 'react'
  3. import type { NextPage } from 'next'
  4. import type { AppProps } from 'next/app'
  5. export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
  6. getLayout?: (page: ReactElement) => ReactNode
  7. }
  8. type AppPropsWithLayout = AppProps & {
  9. Component: NextPageWithLayout
  10. }
  11. export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
  12. // Use the layout defined at the page level, if available
  13. const getLayout = Component.getLayout ?? ((page) => page)
  14. return getLayout(<Component {...pageProps} />)
  15. }

Data Fetching

Inside your layout, you can fetch data on the client-side using useEffect or a library like SWR. Because this file is not a Page, you cannot use getStaticProps or getServerSideProps currently.

  1. // components/layout.js
  2. import useSWR from 'swr'
  3. import Navbar from './navbar'
  4. import Footer from './footer'
  5. export default function Layout({ children }) {
  6. const { data, error } = useSWR('/api/navigation', fetcher)
  7. if (error) return <div>Failed to load</div>
  8. if (!data) return <div>Loading...</div>
  9. return (
  10. <>
  11. <Navbar links={data.links} />
  12. <main>{children}</main>
  13. <Footer />
  14. </>
  15. )
  16. }

For more information on what to do next, we recommend the following sections:

PagesLearn more about what pages are in Next.js.

Custom AppLearn more about how Next.js initialize pages.