Unverified Commit 00ed837b authored by archanaagivale30's avatar archanaagivale30 Committed by GitHub

Updated layout-component with new feature layout.js (#67447)

Hello

This PR updates the layout-component example to use:

- App Router
- layout.js
- Removed customer implementation of layout in _app.js file.

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent 8bd92d9c
import type { NextPageWithLayout } from "./_app"; export default function Home() {
import Layout from "../components/layout";
import Sidebar from "../components/sidebar";
const About: NextPageWithLayout = () => {
return ( return (
<section> <section>
<h2>Layout Example (About)</h2> <h2>Layout Example (About)</h2>
<p> <p>
This example adds a property <code>getLayout</code> to your page, You can define a layout by default exporting a React component from a
allowing you to return a React component for the layout. This allows you layout.js file. The component should accept a children prop that will be
to define the layout on a per-page basis. Since we're returning a populated with a child layout (if it exists) or a page during rendering.
function, we can have complex nested layouts if desired.
</p> </p>
<p> <p>
When navigating between pages, we want to persist page state (input When navigating between pages, we want to persist page state (input
...@@ -31,15 +26,4 @@ const About: NextPageWithLayout = () => { ...@@ -31,15 +26,4 @@ const About: NextPageWithLayout = () => {
</p> </p>
</section> </section>
); );
}; }
export default About;
About.getLayout = function getLayout(page: React.ReactElement) {
return (
<Layout>
<Sidebar />
{page}
</Layout>
);
};
import type { NextPageWithLayout } from "./_app"; export default function Home() {
import Layout from "../components/layout";
import Sidebar from "../components/sidebar";
const Contact: NextPageWithLayout = () => {
return ( return (
<section> <section>
<h2>Layout Example (Contact)</h2> <h2>Layout Example (Contact)</h2>
<p> <p>
This example adds a property <code>getLayout</code> to your page, You can define a layout by default exporting a React component from a
allowing you to return a React component for the layout. This allows you layout.js file. The component should accept a children prop that will be
to define the layout on a per-page basis. Since we're returning a populated with a child layout (if it exists) or a page during rendering.
function, we can have complex nested layouts if desired.
</p> </p>
<p> <p>
When navigating between pages, we want to persist page state (input When navigating between pages, we want to persist page state (input
...@@ -31,15 +26,4 @@ const Contact: NextPageWithLayout = () => { ...@@ -31,15 +26,4 @@ const Contact: NextPageWithLayout = () => {
</p> </p>
</section> </section>
); );
}; }
export default Contact;
Contact.getLayout = function getLayout(page: React.ReactElement) {
return (
<Layout>
<Sidebar />
{page}
</Layout>
);
};
import Sidebar from "./_components/sidebar";
import styles from "./layout.module.css";
export const metadata = {
title: "Layouts Example",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body>
<main className={styles.main}>
<Sidebar />
{children}
</main>
</body>
</html>
);
}
import type { NextPageWithLayout } from "./_app"; export default function Home() {
import Layout from "../components/layout";
import Sidebar from "../components/sidebar";
const Index: NextPageWithLayout = () => {
return ( return (
<section> <section>
<h2>Layout Example (Index)</h2> <h2>Layout Example (Index)</h2>
<p> <p>
This example adds a property <code>getLayout</code> to your page, You can define a layout by default exporting a React component from a
allowing you to return a React component for the layout. This allows you layout.js file. The component should accept a children prop that will be
to define the layout on a per-page basis. Since we're returning a populated with a child layout (if it exists) or a page during rendering.
function, we can have complex nested layouts if desired.
</p> </p>
<p> <p>
When navigating between pages, we want to persist page state (input When navigating between pages, we want to persist page state (input
...@@ -31,15 +26,4 @@ const Index: NextPageWithLayout = () => { ...@@ -31,15 +26,4 @@ const Index: NextPageWithLayout = () => {
</p> </p>
</section> </section>
); );
}; }
export default Index;
Index.getLayout = function getLayout(page: React.ReactElement) {
return (
<Layout>
<Sidebar />
{page}
</Layout>
);
};
import Head from "next/head";
import styles from "./layout.module.css";
type LayoutProps = {
children: React.ReactNode;
};
export default function Layout({ children }: LayoutProps) {
return (
<>
<Head>
<title>Layouts Example</title>
</Head>
<main className={styles.main}>{children}</main>
</>
);
}
import type { NextPage } from "next";
import type { AppProps } from "next/app";
export type NextPageWithLayout = NextPage & {
getLayout?: (page: React.ReactElement) => React.ReactNode;
};
type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};
export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout ?? ((page) => page);
return getLayout(<Component {...pageProps} />);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment