๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป DEV/NextJS

[NEXT.js 14] 5. Layout(feat. { children }: { children: ReactNode }) props

by Rising Oneโ˜… 2024. 6. 18.
728x90
๋ฐ˜์‘ํ˜•
SMALL

 

Layout

  1. ์ •์˜ & ํŠน์„ฑ

 

  2. ์ฝ”๋“œ ์˜ˆ์‹œ

 

  3. ๊ด€๋ จ ์—๋Ÿฌ


์ƒ์„ธ

  1. ์ •์˜ & ํŠน์„ฑ

Next.js ์—๋Š” Layout ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์ด๋Š” ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ๊ฐ„์— ๊ณต์œ ๋˜๋Š” UI์ž…๋‹ˆ๋‹ค.

 

Layout ์ปดํฌ๋„ŒํŠธ๋Š”

- ํŒŒ์ผ์—์„œ React ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๋‚ด๋ณด๋‚ด๋„๋ก ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ณ ,

- ๋ Œ๋”๋ง ์ค‘์— ํ•˜์œ„์— ๋ ˆ์ด์•„์›ƒ(page UI)๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ, ํ•˜์œ„ ์š”์†Œ๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•œ children props๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

 

ใ„ด ์ด๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, { children }: { children: ReactNode } ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ด๋ค„์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

{ children }: { children: ReactNode } ์ด ๊ตฌ๋ฌธ์€ TypeScript์˜ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด์™€ ํƒ€์ž… ์ •์˜๋ฅผ ๊ฒฐํ•ฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

(๋”ฐ๋ผ์„œ, ๊ฐ์ฒด์˜ ํŠน์ • ์†์„ฑ์„ ์ถ”์ถœํ•˜๋ฉด์„œ + ์†์„ฑ์˜ ํƒ€์ž…์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ)

=> ๊ตฌ์กฐ ๋ถ„ํ•ด : ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๊ฐ์ฒด์—์„œ "children" ์†์„ฑ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

=> ํƒ€์ž… ์ •์˜ : "children" ์†์„ฑ์˜ Type์ด "ReactNode"์ž„์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

 


 

  2. ์ฝ”๋“œ ์˜ˆ์‹œ

 

# Layout.tsx ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ

export default function DashboardLayout({ children }: { children: React.ReactNode }) {
  return (
    <section>
      {children}
    </section>
  )
}

 

# ๋˜ ๋‹ค๋ฅธ ๊ฒฝ์šฐ (๋ช…์‹œ)

import React, { ReactNode } from 'react';

interface RootLayoutProps {
  children: ReactNode;
}

export default function RootLayout({ children }: RootLayoutProps) {
  return (
    <html lang="en">
      <body>
        {children}
      </body>
    </html>
  );
}

 

  3. ๊ด€๋ จ ์—๋Ÿฌ

TS2559

 

Layout ์ปดํฌ๋„ŒํŠธ์— ๋ ˆ์ด์•„์›ƒ(page UI)๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ, ํ•˜์œ„ ์š”์†Œ๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•œ children props๋ฅผ ์ž‘์„ฑํ•ด ํ•ด๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.


 

728x90
๋ฐ˜์‘ํ˜•
LIST

๋Œ“๊ธ€