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

[Next.js 13] ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ํ›„, page์—์„œ ์ปดํฌ๋„ŒํŠธ์— <HTML> ํƒœ๊ทธ, ์†์„ฑ๊ฐ’ ์‚ฌ์šฉ๋ฒ•

by Rising Oneโ˜… 2023. 9. 27.
728x90
๋ฐ˜์‘ํ˜•
SMALL

NextJS ๋Œ€ํ‘œ๋กœ๊ณ 

 


์ ‘๊ทผ (์š”์•ฝ)

ํ˜„์žฌ ๊ฐœ๋ฐœ์ค‘์ธ ํ”„๋กœ์ ํŠธ์—์„œ Next.js 13๋ฒ„์ „๊ณผ TypeScript ๋ฅผ ๊ธฐ์ˆ ์Šคํƒ์œผ๋กœ ๊ฐ–๊ณ , ๊ตฌ์„ฑ์š”์†Œ๋งˆ๋‹ค ์ตœ๋Œ€ํ•œ ์ž˜๊ฒŒ ์ปดํฌ๋„ŒํŠธํ™”๋ฅผ ๋ชฉํ‘œ๋กœ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (1) ์ปดํฌ๋„ŒํŠธ(components)๋ฅผ ๋นผ์ฃผ๊ณ , ์‹ค์ œ ํ™”๋ฉด์„ ๋ณด์—ฌ์ฃผ๋Š” (2) page.tsx ํŒŒ์ผ์—์„œ (1) ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ (3) ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ ๋‚ด์— <html> ํƒœ๊ทธ๋‚˜ ์†์„ฑ๊ฐ’์„ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

 

  1. (์ƒํ™ฉ) ์ปดํฌ๋„ŒํŠธํ™” & page.tsx ์ƒ์„ฑ

      (1.1). ์ปดํฌ๋„ŒํŠธํ™” + ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ์„ ์œ„ํ•œ ๋“ฑ๋ก

      (1.2). Page.tsx ์ƒ์„ฑ + ์ปดํฌ๋„ŒํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ

 

 

  2. ์ปดํฌ๋„ŒํŠธ์— ์†์„ฑ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

      (2.1). ์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ (interface, props)

      (2.2). page.tsx( ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ• ) ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ

 

  3. ์ปดํฌ๋„ŒํŠธ์— <HTML> ํƒœ๊ทธ ์ „๋‹ฌํ•˜๊ธฐ

      (3.1). ์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ (interface, props)

      (3.2). page.tsx( ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ• ) ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ


๋ฐฉ๋ฒ•

  1. (์ƒํ™ฉ) ์ปดํฌ๋„ŒํŠธํ™” & page.tsx ์ƒ์„ฑ

      (1.1). ์ปดํฌ๋„ŒํŠธํ™” + ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ์„ ์œ„ํ•œ ๋“ฑ๋ก

 

- "์ปดํฌ๋„ŒํŠธ.tsx" ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๊ณ 

- ์ด๋ฅผ ์ „์—ญ์ ์œผ๋กœ export ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋งŒ๋“ค์–ด ๋‘” index.ts ํŒŒ์ผ ๋“ฑ์— ๋“ฑ๋ก์„ ํ•ด์ค๋‹ˆ๋‹ค.

 

import ์ปดํฌ๋„ŒํŠธ๋ช… from './์ปดํฌ๋„ŒํŠธ๋ช…';

export {
	์ปดํฌ๋„ŒํŠธ๋ช…,
}

 

 

      (1.2). Page.tsx ์ƒ์„ฑ + ์ปดํฌ๋„ŒํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ

'use client';

import React from 'react';
import { ์ปดํฌ๋„ŒํŠธ๋ช… } from '@/components';

export default function ํŽ˜์ด์ง€๋ช…() {
  return (
    <div>
      <div className="">
        <div>
          <์ปดํฌ๋„ŒํŠธ๋ช… />
        </div>
      </div>
    </div>
  );
}

 


  2. ์ปดํฌ๋„ŒํŠธ์— ์†์„ฑ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

      (2.1). ์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ (interface, props)

 

- ์ปดํฌ๋„ŒํŠธ์— ์†์„ฑ์„ ์ „๋‹ฌํ•˜๋ ค๋ฉด ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์—์„œ props๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ์„ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

- ์˜ˆ๋ฅผ ๋“ค์–ด, ์ œ๋ชฉ์„ ์†์„ฑ์œผ๋กœ ๋ฐ›๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

 

// ์ปดํฌ๋„ŒํŠธ๋ช….tsx

import React from 'react';

interface ์ปดํฌ๋„ŒํŠธ๋ช…Props {
  title: string; // title ์†์„ฑ์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ›์Œ
}

function ์ปดํฌ๋„ŒํŠธ๋ช…({ title }: ์ปดํฌ๋„ŒํŠธ๋ช…Props) {
  return (
    <div>
      <div className="">{title}</div>
    </div>
  );
}

export default ์ปดํฌ๋„ŒํŠธ๋ช…;

 

      (2.2). page.tsx( ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ• ) ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ

- ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์†์„ฑ title์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

// page.tsx

import React from 'react';
import { ์ปดํฌ๋„ŒํŠธ๋ช… } from '@/components';

export default function Page() {
  return (
    <div>
      <div className="~~~">
        <div>
          {/* title ์†์„ฑ์„ ์ „๋‹ฌ */}
          <์ปดํฌ๋„ŒํŠธ๋ช… title="OOOOO" />
        </div>
      </div>
    </div>
  );
}

  3. ์ปดํฌ๋„ŒํŠธ์— <HTML> ํƒœ๊ทธ ์ „๋‹ฌํ•˜๊ธฐ

      (3.1). ์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ (interface, props + children)

// ์ปดํฌ๋„ŒํŠธ.tsx

'use client';

import React from 'react';

interface ์ปดํฌ๋„ŒํŠธ๋ช…Props {
  children: React.ReactNode; // ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์˜ ์ž์‹ ์š”์†Œ๋ฅผ ๋ฐ›์Œ
}

function PageTitle({ children }: ์ปดํฌ๋„ŒํŠธ๋ช…Props) {
  return (
    <div>
      <div className="~~~">{children}</div>
    </div>
  );
}

export default ์ปดํฌ๋„ŒํŠธ๋ช…;

 

      (3.2). page.tsx( ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ• ) ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ

'use client';

import React from 'react';
import { ์ปดํฌ๋„ŒํŠธ๋ช… } from '@/components';

export default function ํŽ˜์ด์ง€๋ช…() {
  return (
    <div>
      <div className="~~~">
        <div>
          <์ปดํฌ๋„ŒํŠธ๋ช…>
          โ˜…1. ๋‹จ์ˆœํ•œ ๋ฌธ์ž์—ด ์ž‘์„ฑ ๊ฐ€๋Šฅ : Page ์ž…๋‹ˆ๋‹ค.
          โ˜…2. <a></a>, <p></p> ๋“ฑ HTML ํƒœ๊ทธ ์ž‘์„ฑ ๊ฐ€๋Šฅ
          </์ปดํฌ๋„ŒํŠธ๋ช…>
        </div>
      </div>
    </div>
  );
}

 


์ด์ƒ์œผ๋กœ, NextJS ๋‚ด TailwindCSS ์‚ฌ์šฉ์‹œ ์ปดํฌ๋„ŒํŠธ์— HTML ํƒœ๊ทธ ์ถ”๊ฐ€ ๋ฐ ์†์„ฑ๊ฐ’ ์ „๋‹ฌ์„ ํ•™์Šตํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์งˆ๋ฌธ์„ ๋Œ“๊ธ€์„ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค^^

์˜ค๋Š˜๋„ ํŒŒ์ดํŒ…์ž…๋‹ˆ๋‹ค! 

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

๋Œ“๊ธ€