Created
February 15, 2023 05:31
-
-
Save DennisKo/4fa80a437f5b5acbfe5f85183d6014f4 to your computer and use it in GitHub Desktop.
NextJS per-page layout with default layout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { type AppProps } from "next/app"; | |
import { type Session } from "next-auth"; | |
import { SessionProvider } from "next-auth/react"; | |
import { api } from "@/utils/api"; | |
import "@/styles/globals.css"; | |
import AppLayout from "@/components/AppLayout"; | |
import type { ReactElement, ReactNode } from "react"; | |
import type { NextPage } from "next"; | |
export type NextPageWithLayout<P = object, IP = P> = NextPage<P, IP> & { | |
getLayout?: (page: ReactElement) => ReactNode; | |
}; | |
type AppPropsWithLayout = AppProps<{ session: Session | null }> & { | |
Component: NextPageWithLayout; | |
}; | |
const MyApp = ({ | |
Component, | |
pageProps: { session, ...pageProps }, | |
}: AppPropsWithLayout) => { | |
const getLayout = | |
Component.getLayout ?? ((page) => <AppLayout>{page}</AppLayout>); | |
const layout = getLayout(<Component {...pageProps} />); | |
return ( | |
<SessionProvider session={session}> | |
{layout} | |
</SessionProvider> | |
); | |
}; | |
export default api.withTRPC(MyApp); | |
// example Page | |
Home.getLayout = function (page) { | |
return <Otherlayout>{page}</Otherlayout>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment