Created
September 22, 2021 18:30
-
-
Save danim47c/47096312db25ffe5f6239c69e41e43dc to your computer and use it in GitHub Desktop.
Layouts util for Next.js
This file contains 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 { 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 |
This file contains 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 { 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