Data sharing (Solving props drilling)

next.js 13.4 ์ด์ƒ App router ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

props drilling ์˜ˆ์‹œ

next.js์—์„œ ์ฃผ๋กœ ๋ฐœ์ƒํ•˜๋Š” props drilling ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๋จผ์ € ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ fetch ํ•ฉ๋‹ˆ๋‹ค. API ๋…ธ์ถœ ๋ฐฉ์ง€, ์ตœ์ ํ™” ๋“ฑ ๋‹ค์–‘ํ•œ ์ด์œ ๋กœ ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ(์„œ๋ฒ„ ์‚ฌ์ด๋“œ)์—์„œ API๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
// Server Component A
import ServerComponentB from "./ServerComponentB"
 
export default async function ServerComponentA() {
  const initialCount = await fetch(...);
  
  return <>
     <h3>I got {initialCount}</h3>
     <ServerComponentB initialCount={initialCount} />
  </>
}
  • ๋ฐ์ดํ„ฐ๋ฅผ fetchํ•œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ ํ•˜์œ„ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค. ServerComponentB์—์„œ๋Š” initialCount๊ฐ€ ํ•„์š”์—†์ง€๋งŒ, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•„์š”ํ•˜๊ธฐ์— ๋ฐ›๊ณ , ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.
// Server Component B
import ServerComponentC from "./ServerComponentC"
 
export default function ServerComponentB({ initialCount }) {
  return <ServerComponentC initialCount={initialCount}/>
}
  • ServerComponentC์—์„œ๋Š” initialCount๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ ํ›„ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—๋„ initialCount๊ฐ€ ํ•„์š”ํ•˜๊ธฐ์— props๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.
// Server Component C
import ClientComponent from "./ClientComponent"
 
export default function ServerComponentC({ initialCount }) {
  return (
    <div>
      <h3>initial count is : {initialCount}</h3>
    	<ClientComponent initialCount={initialCount}/>
  	</div>
  )
}
  • ์ตœ์ข…์ ์œผ๋กœ ์ตœํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— ๋„๋‹ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.
// Client Component
'use client'
 
import {useState} from "react"
 
export default function ClientComponent({initialCount}) {
  const [count, setCount] = useState(initialCount);
  
  return (
    <>
      {count}
      <button onClick={() => setCount(prev => prev + 1)}>+1</button>
      <button onClick={() => setCount(prev => prev - 1)}>-1</button>
    </>
  )
}

์ด์ฒ˜๋Ÿผ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ๊ฐ€์ ธ์•ผํ•˜๊ณ  ์ด๊ฒƒ์„ props๋กœ ๋„˜๊ฒจ์ค„๋•Œ, props drilling์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ค‘๊ฐ„์— ServerComponentB์—์„œ๋Š” initialCount๊ฐ€ ํ•„์š”์—†์—ˆ์ง€๋งŒ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•„์š”ํ•˜๊ธฐ์— ๋ฐ›๊ณ  ๋„˜๊ฒจ์ค˜์•ผํ–ˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ผ๋ฐ˜์ ์ธ React ์•ฑ์ฒ˜๋Ÿผ ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค๋ฉด ๊ธ€๋กœ๋ฒŒ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(redux, recoil..)๋‚˜, Context API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ์— ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์€ ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋Š” ๊ตฌ์กฐ์—์„œ props drilling ๋ฌธ์ œ ์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Server Component โ†’ Server Component

next.js์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ธ Automatic fetch() Requst Deduping์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. next.js ๊ณต์‹๋ฌธ์„œ์—์„œ์˜ ์„ค๋ช…์— ๋”ฐ๋ฅด๋ฉด, ์„œ๋ฒ„์—์„œ fetch๋กœ GETํ•œ ๋ฐ์ดํ„ฐ๋Š” rendering pass ๋™์•ˆ ์บ์‹œ๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์บ์‹œ๋Š” server request ์ƒ๋ช…์ฃผ๊ธฐ๋™์•ˆ ์œ ์ง€๋˜๋ฉฐ ๋ Œ๋”๋ง ๊ณผ์ •์ด ๋๋‚ ๋•Œ๊นŒ์ง€ ์ง€์†๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋ผ๋ฆฌ๋Š” ๋ณ„๋„์˜ data sharing ๊ธฐ๋ฒ•์ด ํ•„์š”ํ•˜์ง€ ์•Š๊ณ , ๋งค๋ฒˆ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

// Server Component A
import ServerComponentB from "./ServerComponentB"
 
export default async function ServerComponentA() {
  const initialCount = await fetch(...);
  
  return <>
     <h3>I got {initialCount}</h3>
     <ServerComponentB/>
  </>
}
// Server Component B
import ServerComponentC from "./ServerComponentC"
 
export default function ServerComponentB() {
  return <ServerComponentC/>
}
// Server Component C
import ClientComponent from "./ClientComponent"
 
export default function ServerComponentC() {
  const initialCount = await fetch(...);
  return (
    <div>
      <h3>initial count is : {initialCount}</h3>
    	<ClientComponent initialCount={initialCount}/>
  	</div>
  )
}

์ด์ œ ServerComponentB์—์„œ๋Š” initialCount๋ฅผ ๋ฐ›์„ ํ•„์š”๋„, ๋„˜๊ฒจ์ค„ ํ•„์š”๋„ ์—†์Šต๋‹ˆ๋‹ค. fetch() ๋™์•ˆ ๊ฒฐ๊ณผ๊ฐ€ ์บ์‹œ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‘๋ฒˆ์ด์ƒ API ์š”์ฒญ์„ ๋ณด๋‚ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ•œ๊ณ„์ 

์ €๋Š” ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ API ์š”์ฒญ์„ ๋ณด๋‚ผ๋•Œ ์ฃผ๋กœ searchParams์— ๋“ค์–ด์˜จ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ API๋ฅผ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค.

// ex : ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ฃผ์†Œ -> /notice?categoryId=4&searchValue=test
// categoryId=4, searchValue=test ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ด
export default async function NoticePage({ searchParams : {categoryId, searchValue } }) {
  const notices = await getNotices({ categoryId, searchValue })
  ...
}

ํ•˜์ง€๋งŒ next.js app router์—์„œ page.js๊ฐ€ ์•„๋‹Œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ searchParams๋ฅผ ์–ป์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ณต์‹์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์—์„œ ์„ค๋ช…ํ•œ, ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ api๋ฅผ ์š”์ฒญํ•˜๋Š” ๊ฑด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. API ์š”์ฒญ์— ํ•„์š”ํ•œ searchParams๋ฅผ ์–ป์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

// page.js๊ฐ€ ์•„๋‹Œ ์–ด๋”˜๊ฐ€์˜ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ
export default function NoticeList() {
  // error! categoryId, searvhValue๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์—†์Œ
  const notices = await getNotices({ categoryId, searchValue })
}

๋”ฐ๋ผ์„œ Server Component -> Server Component data sharing์„ ์ข€ ๋” ์ž˜ ํ™œ์šฉํ•˜๋ ค๋ฉด, ํ•˜๋‚˜์˜ Request๊ฐ€ ์‚ด์•„์žˆ๋Š” ๋™์•ˆ ์—ฌ๋Ÿฌ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ ๊ทธ Request์˜ searchParams์— ์ ‘๊ทผ๊ฐ€๋Šฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๊ณต์‹์ ์ธ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋‚˜ ๋ช‡๊ฐ€์ง€ ์ƒ๊ฐํ•ด๋ณธ/์ฐพ์•„๋ณธ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์›น์„œ๋ฒ„ ํ™œ์šฉ

    next.js์—์„œ๋Š” ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์— headers()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์š”์ฒญ์ด ์˜จ Request์˜ header์„ ์ œ๊ณตํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊ผญ page.js๋งŒ์ด ์•„๋‹Œ ๊ณณ์—์„œ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๊ธฐ์—, header์— searchParams๊ฐ€ ๋“ค์–ด์žˆ๋‹ค๋ฉด page.js๊ฐ€ ์•„๋‹Œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ์—์„œ๋„ searchParams์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

    # nginx ์˜ˆ์‹œ
    server {
        location / {
            proxy_set_header X-Search-Params $args;
            proxy_pass http://localhost:3000;
        }
    }
    import { headers } from 'next/headers'
     
    export default function NoticeList() {
      const header = headers() 
      const { categoryId, searchValue } = parseSearchParams(header.get("X-Search-Params"))
      
      const notices = await getNotices({ categoryId, searchValue })
    }
  2. searchParams ์ •๋„๋Š” props๋กœ ๋‚ด๋ ค๋ณด๋‚ด๊ธฐ

    page.js์—์„œ searchParams๊ฐ€ ํ•„์š”ํ•œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๊นŒ์ง€ ๋‚ด๋ ค์ค˜์•ผํ•˜์ง€๋งŒ, searchParams ๊ฐ์ฒด ํ•˜๋‚˜๋งŒ ๋‚ด๋ ค์ฃผ๋ฉด ๋˜๊ธฐ์— props drilling์„ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ ๋‚ด๋ ค๋ณด๋‚ด๋Š” ์ˆ˜์ค€์œผ๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    export default function NoticeList({searchParams : {categoryId, searchValue }}) {
      const notices = await getNotices({ categoryId, searchValue })
    }

    ๊ด€๋ จํ•ด์„œ next.js ๊นƒํ—ˆ๋ธŒ discussions์—์„œ ๋…ผ์˜๋œ ์–˜๊ธฐ๊ฐ€ ์žˆ๋Š”๋ฐ, searchParams์— ๋Œ€ํ•œ validation/parsing/error handling ์—†์ด ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ searchParams๋ฅผ ๋„˜๊ฒจ์ค„ ๊ฐ€๋Šฅ์„ฑ์€ ์ ๋‹ค๋Š” ๋Œ“๊ธ€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋Šฅ์„ฑ์ด ์ ์€์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜, ํ™•์‹คํžˆ ํŒŒ์‹ฑ, ์œ ํšจ์„ฑ ๊ฒ€์ฆ์ด ํ•„์š”ํ•œ searchParams์˜ ๊ฒฝ์šฐ page.js์—์„œ ๋จผ์ € ๋ฐ›์•„ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ ํ›„, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ props๋กœ ๋„˜๊ฒจ์ฃผ๋Š”๊ฒŒ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค.

  3. ์„œ๋ฒ„๋‹จ์—์„œ data sharing ํˆด ์‚ฌ์šฉ

    ๊ผญ searchParams์— ๊ตญํ•œ๋˜์ง€ ์•Š๋”๋ผ๋„, ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋“ค ์‚ฌ์ด์—์„œ ๋ฐ์ดํ„ฐ or context๋ฅผ ๊ณต์œ ํ•˜๋ฉด ์ข‹์€ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. (i18n, theme ๋“ฑ). next.js discussions

    ๊ณต์‹์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์€ ์—†์ง€๋งŒ, ์„œ๋“œํŒŒํ‹ฐ ํˆด์„ ๋งŒ๋“œ๋ ค๋Š” ์‹œ๋„๋Š” ์กด์žฌํ•ฉ๋‹ˆ๋‹ค(server-only-context, react cache function ํ™œ์šฉ). ๊ณต์‹๋ฌธ์„œ์—์„œ๋Š” db์™€ ๊ฐ™์ด ๋„ค์ดํ‹ฐ๋ธŒ JS ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ถ”์ฒœํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ

Client Component โ†’ Client Component

์ž์ฃผ ์žˆ๋Š” ์ƒํ™ฉ์œผ๋กœ, ์–ด๋–ค ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ์™€ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ ๋จผ ๊ณณ์— ์žˆ๋Š” ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐ์ดํ„ฐ ๋˜๋Š” ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌ์•กํŠธ ์•ฑ์„ ๊ฐœ๋ฐœํ• ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ „์—ญ ์ƒํƒœ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•์„ ์ฑ„ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. next.js ๊ณต์‹๋ฌธ์„œ์—์„œ๋„ Context API ๋ฐ ์„œ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์„ค๋ช…์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ

์˜ˆ์‹œ๋กœ๋Š” Context API๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

// layout.tsx
import { AppProvider } from '../lib/AppProvider';
 
export default function RootLayout({ children }) {
    return (
        <html lang="en">
            <body className={inter.className}>
                <AppProvider>{children}</AppProvider>
            </body>
        </html>
    );
}
// lib/AppProvider.tsx
'use client';
import React, { createContext, useState, useContext } from 'react';
 
const AppContext = createContext();
 
export const AppProvider = ({ children }) => {
    const [state, setState] = useState(0);
    const value = {
        state,
        setState,
    };
    return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
}
export const useAppContext = () => useContext(AppContext);
// page.tsx
import Counter from '../components/counter';
import ServerComp from '../components/server';
 
export default function Home() {
    return (
        <main className={styles.main}>
            <ServerComp />
            <Counter />
        </main>
    );
}
// components/counter.tsx
'use client';
 
import { useAppContext } from '../lib/AppProvider';
 
export default function Counter() {
    const { state, setState } = useAppContext();
 
    const increaseHandler = () => {
        setState(state + 1);
    };
 
    const decreaseHandler = () => {
        setState(state - 1);
    };
    // Use the state and setState as needed
    return (
        <>
            <div>{state}</div>
            <a href="#" onClick={increaseHandler}>
                increase
            </a>{' '}
            &nbsp;
            <a href="#" onClick={decreaseHandler}>
                decrease
            </a>
        </>
    );
}
  • ์˜คํ•ด : Context Provider๋Š” ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ์ด๊ณ , ์ด๊ฒƒ์„ ๊ฐ€์žฅ ์ƒ์œ„์— ๋‘๋ฉด ๊ทธ ์•„๋ž˜๋Š” ์ „๋ถ€ ํด๋ผ์ด์–ธํŠธ ๋ฒˆ๋“ค์— ํฌํ•จ๋ ๊ฑฐ๋ผ๊ณ  ์˜คํ•ดํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” next.js ๊ณต์‹๋ฌธ์„œ์— ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ๊ตฌ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ์š”.

Once "use client" is defined in a file, all other modules imported into it, including child components, are considered part of the client bundle

https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive

ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋“ค์€ ํด๋ผ์ด์–ธํŠธ ๋ฒˆ๋“ค๋กœ ๊ฐ„์ฃผ๋œ๋‹ค๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Context API Provider๋ฅผ ์ƒ์œ„์— ๋‘๋ฉด ๊ทธ ํ•˜์œ„ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋“ค๋„ ๋ชจ๋‘ ํด๋ผ์ด์–ธํŠธ ๋ฒˆ๋“ค์— ํฌํ•จ๋˜์–ด ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ์—†์–ด์งˆ๊ฒƒ๋งŒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์‹คํ—˜ํ•ด๋ณธ ๊ฒฐ๊ณผ, ํด๋ผ์ด์–ธํŠธ ๋ฒˆ๋“ค์— ํฌํ•จ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ด์œ ๋Š” ServerComponent๊ฐ€ ClientComponent(Context Provider)์— ์ง์ ‘ import ๋˜์ง€ ์•Š๊ณ , props๋กœ ๋„˜์–ด๊ฐ”๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค

next.js ๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋ฅผ ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ ๋ฐ‘์— ๋‘๋ ค๋ฉด, import ํ•˜์ง€ ๋ง๊ณ  prop(children)์œผ๋กœ ๋„˜๊ฒจ์ค˜์•ผํ•œ๋‹ค๊ณ  ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ

// Unsupported Pattern: Importing Server Components into Client Components
 
'use client'
 
// This pattern will **not** work!
// You cannot import a Server Component into a Client Component.
import ExampleServerComponent from './example-server-component'
 
export default function ExampleClientComponent({
  children,
}: {
  children: React.ReactNode
}) {
  const [count, setCount] = useState(0)
 
  return (
    <>
      <button onClick={() => setCount(count + 1)}>{count}</button>
      <ExampleServerComponent />
    </>
  )
}
// Recommended Pattern: Passing Server Components to Client Components as Props
 
'use client'
 
import { useState } from 'react'
 
export default function ExampleClientComponent({
  children,
}: {
  children: React.ReactNode
}) {
  const [count, setCount] = useState(0)
 
  return (
    <>
      <button onClick={() => setCount(count + 1)}>{count}</button>
 
      {children}
    </>
  )
}

๊ณต์‹ ๋ฌธ์„œ์— ์„ค๋ช…๋˜์–ด ์žˆ๋Š”๋Œ€๋กœ, import ๋Œ€์‹  props(children)์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์œ„ํ•œ slot์„ ๋‚จ๊ธฐ๊ณ , ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๊ทธ slot์— ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹๋Œ€๋กœ ํ•œ๋‹ค๋ฉด ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋Š” ํด๋ผ์ด์–ธํŠธ ๋ฒˆ๋“ค์— ํฌํ•จ๋˜์ง€ ์•Š๊ณ , ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋กœ์จ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

Server Component โ†’ Client Component

โ€œServer Component โ†’ Server Componentโ€์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, ํ•œ๋ฒˆ ๋ถ€๋ฅธ fetch์˜ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์€ ์š”์ฒญ์ด ์‚ด์•„์žˆ๋Š” ๋™์•ˆ ์บ์‹œ๋œ๋‹ค๋Š” ๊ฒƒ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋กœ ํ•œ๋ฒˆ ๊ฐ์‹ธ์„œ props drilling์„ ์™„ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋งํฌ(์œ ํŠœ๋ธŒ ์˜์ƒ, ์†Œ๋ฆฌ์ฃผ์˜)

// Server Component A
import ServerComponentB from "./ServerComponentB"
 
export default async function ServerComponentA() {
  const initialCount = await fetch(...);
  
  return <>
     <h3>I got {initialCount}</h3>
     <ServerComponentB />
  </>
}
// Server Component B
import ServerComponentC from "./ServerComponentC"
 
export default function ServerComponentB() {
  return <ServerComponentC/>
}
// Server Component C
import ClientComponent from "./ClientComponent"
 
export default function ServerComponentC() {
  const initialCount = await fetch(...);
  return 	<ClientComponent initialCount={initialCount}/>
}
// Client Component
'use client'
 
import {useState} from "react"
 
export default function ClientComponent({initialCount}) {
  const [count, setCount] = useState(initialCount);
  
  return (
    <>
      {count}
      <button onClick={() => setCount(prev => prev + 1)}>+1</button>
      <button onClick={() => setCount(prev => prev - 1)}>-1</button>
    </>
  )
}

Client Component โ†’ Server Component

props๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‚˜, ๋‹ค์Œ๊ณผ ๊ฐ™์ด cloneElement๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

import React from "react";
import ClientComponent from "./ClientComponent"
 
export default function ParentComponent({ children }) {
  return (
    <ClientComponent>
       <ServerComponent/>
    </ClientComponent>
  ) 
}
'use client'
 
import React from "react";
 
export default function ClientComponent({ children }) {
  return (
    <div>
       {React.cloneElement(children, {name:"test props"})}
    </div>
  ) 
}
import React from "react";
 
export default function ServerComponent({ name }) {
  return (
    <div>
       {name}
    </div>
  ) 
}

ํ•˜์ง€๋งŒ ๊ฐ€๋Šฅํ•œ ๊ฒƒ๊ณผ๋Š” ๋ณ„๊ฐœ๋กœ ํฌ๊ฒŒ ํšจ์šฉ์„ฑ์ด ์žˆ์„์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ์˜ ๋ฐ”๋กœ ํ•œ๋‹จ๊ณ„ ๋ฐ‘ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋กœ๋งŒ ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ์—, ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด URL ์ฟผ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์ƒˆ๋กœ ํŽ˜์ด์ง€๋ฅผ fetchํ•˜๋Š”๊ฒŒ ๋‚˜์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. URL ์ฟผ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ ํ•œ๋‹จ๊ณ„ ๋ฐ‘ ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ด€๋ จ์žˆ๋Š” ๋ชจ๋“  ์„œ๋ฒ„์ปดํฌ๋„ŒํŠธ๋“ค๋„ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋–„๋ฌธ์ž…๋‹ˆ๋‹ค.