[NextJS] Layout & Head

๊ฐ•์€๋น„ยท2022๋…„ 6์›” 13์ผ
0

NextJS

๋ชฉ๋ก ๋ณด๊ธฐ
4/10
post-thumbnail

NextJS ๊ณต์‹๋ฌธ์„œ ๐Ÿ‘

Layout

navigation์ด๋‚˜ footer์™€ ๊ฐ™์€ ๊ณตํ†ต ๋ ˆ์ด์•„์›ƒ์€ Layout component๋กœ ๋งŒ๋“ค๊ณ  _app.tsx์— importํ•˜์—ฌ ๋ Œ๋”๋งํ•œ๋‹ค.

Example

// components/Layout.tsx

import React from "react";
import NavBar from "./NavBar";

const Layout = ({ children }: React.PropsWithChildren) => {
  return (
    <div>
      <NavBar />
      <div>{children}</div>
    </div>
  );
};

export default Layout;
// components/Layout.tsx

import React from "react";
import NavBar from "./NavBar";

const Layout = ({ children }: React.PropsWithChildren) => {
  return (
    <div>
      <NavBar />
      <div>{children}</div>
    </div>
  );
};

export default Layout;

Head

Head component์˜ children์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ํƒœ๊ทธ๋“ค์ด html์˜ head ์•ˆ์— ๋ณด์—ฌ์งˆ ๊ฒƒ์ด๋‹ค.
Head component๋Š” html์˜ head ํƒœ๊ทธ์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•œ๋‹ค.

// components/CustomHead.tsx

import Head from "next/head";

interface CustomHeadProps {
  title: string;
}

const CustomHead = ({ title }: CustomHeadProps) => {
  return (
    <Head>
      <title>{title} | Next Movies</title>
    </Head>
  );
};

export default CustomHead;
// pages/index.tsx

import type { NextPage } from "next";
import CustomHead from "../components/CustomHead";

const Home: NextPage = () => {
  return (
    <div>
      <CustomHead title="Home" />
      <h1>Home</h1>
    </div>
  );
};

export default Home;
// pages/about.tsx

import type { NextPage } from "next";
import CustomHead from "../components/CustomHead";

const About: NextPage = () => {
  return (
    <div>
      <CustomHead title="About" />
      <h1>About</h1>
    </div>
  );
};

export default About;

0๊ฐœ์˜ ๋Œ“๊ธ€