Skip to content

Instantly share code, notes, and snippets.

@danim47c
Created September 22, 2021 18:30
Show Gist options
  • Save danim47c/47096312db25ffe5f6239c69e41e43dc to your computer and use it in GitHub Desktop.
Save danim47c/47096312db25ffe5f6239c69e41e43dc to your computer and use it in GitHub Desktop.
Layouts util for Next.js
import { FC } from "react"
import { AppProps, RenderLayout } from "utils/layouts"
const App: FC<AppProps> = ({ Component, ...props }) => {
return (
<RenderLayout Layout={Component.Layout} props={props}>
<Component {...props.pageProps} />
</RenderLayout>
)
}
export default App
import { NextPage } from "next"
import { AppProps as BaseAppProps } from "next/app"
import { FC } from "react"
export type AppProps = BaseAppProps & { Component: Page }
export type Page = NextPage & { Layout?: Layout }
export type LayoutProps = Omit<AppProps, "Component">
export type Layout = FC<LayoutProps> & { Layout?: Layout }
export const RenderLayout: FC<{ Layout?: Layout; props: LayoutProps }> = ({
Layout: BaseLayout,
props,
children,
}) => {
let comp = <>{children}</>
if (BaseLayout) {
comp = <BaseLayout {...props}>{children}</BaseLayout>
if (BaseLayout?.Layout)
comp = (
<RenderLayout Layout={BaseLayout.Layout} props={props}>
{comp}
</RenderLayout>
)
}
return comp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment