๐ NextJS ํ๊ฒฝ์์, ์ฝ๋์ createContext๊ฐ ์๋๋ฐ, createContext only works in Client Components.๊ฐ์ ์๋ฌ๊ฐ ๋๋ ์ด์ ๋ฐ ํด๊ฒฐ ๊ณผ์ - codesquad-masters2025-team01/issue-tracker GitHub Wiki
-
์ด๊ธฐ ์ธํ ์ ์ผ์ถ ๋ง๋ฌด๋ฆฌํ๊ณ ์ฑ์ ๋๋ ค๋ดค๋๋ฐ, createContext only works in Client Components.๊ฐ์ ์๋ฌ๊ฐ ๋๋ค.
-
"use client" ๋ช ์์๋ฅผ ๋ฌ๋ผ๊ณ ํ๋๋ฐ, ๊ทผ๋ฐ ๋ด ์ฝ๋์ createContext๊ฐ ์๋๋ฐ..?
์๋ฌ๋ฉ์ธ์ง
TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
https://nextjs.org/docs/messages/context-in-server-component
at eval (webpack-internal:///(rsc)/./node_modules/@emotion/react/dist/emotion-element-782f682d.development.esm.js:36:77)
at <unknown> (rsc)/./node_modules/@emotion/react/dist/emotion-element-782f682d.development.esm.js
(/Users/sangyoonlee1231/Desktop/codeSquad/groupProject/issueTracker/issue-tracker-main/frontend/.next/server/vendor-chunks/@emotion.js:70:1)
at ... (์ดํ ์๋ต)
-
@emotion/react ๋ด๋ถ ์ปดํฌ๋ํธ(์: <Global>, <ThemeProvider>)๋ React.createContext๋ฅผ ์ฌ์ฉํ๋ค๊ณ ํ๋ค.
๊ทธ๋ฐ๋ฐ ์ด๋ฅผ app/layout.tsx(Server Component) ์์์ ๋ฐ๋ก importํด์ ์ฌ์ฉํ๋ฉด ์๋ฒ ๋ ๋๋ง ๋จ๊ณ์์ createContext๊ฐ ์คํ๋ผ ๋ฒ๋ฆฌ๊ณ ,
Next JS๊ฐ โcreateContext๋ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์๋ง ๋ฉ๋๋คโ๋ผ๋ฉฐ ์๋ฌ๋ฅผ ํฐ๋จ๋ฆฐ๋ค. -
โ์ฝ๋์ createContext๊ฐ ์๋๋ฐ?โ โ ์ง์ ํธ์ถํ ์ ์ด ์์ด๋
import { Global } from '@emotion/react'
๊ฐ์ ์ฝ๋๊ฐ ์์ผ๋ฉด
โ @emotion/react ๋ด๋ถ์์ createContext๋ฅผ ์ด๋ค๊ณ ํ๋ค. -
์ฆ, NextJS์ Emotion์ ๊ฐ์ด ์ฌ์ฉํ์ ๋ ๋ฐ์ํ ์ ์๋ ์๋ฌ
โ์ 'use client' ๋ฅผ ๋ถ์ฌ์ผ ํ ๊น?โ
์๋ 4๋จ๊ณ๋ง ๊ธฐ์ตํ๋ฉด ์ค๋ฅ์ ์์ธ์ ๋ฐ๋ก ์ ์ ์์ด์!
const Box = styled.div`
color: red;
padding: 8px;
`;
- ์ด ์ฝ๋๋ JS ๊ฐ์ฒด โ ๋ฌธ์์ด๋ก ๋ณํ ์ค๋น๋ฅผ ํด๋ก๋๋ค.
- (์ ํ) Babel ํ๋ฌ๊ทธ์ธ์ ์ฐ๋ฉด ์ ์ ์ธ ๋ถ๋ถ์ ๋ฏธ๋ฆฌ ๊ณ์ฐํด ์๋๊ฐ ๋นจ๋ผ์ ธ์.
๋ ๋๋ง์ด ์์๋๋ฉด Emotion์ด:
- ์คํ์ผ์ ํ ์ค์ง๋ฆฌ ๋ฌธ์์ด๋ก ๋ง๋ญ๋๋ค. (
color:red;padding:8px;
) - ๊ฐ์ ๋ฌธ์์ด์ด ๋ ๋์ค๋ฉด ์ฌํ์ฉํ๊ธฐ ์ํด ํด์(
css-1a2b3c
)๋ฅผ ๋ถ์ฌ์.
๊ฐ์ ์คํ์ผ = ๊ฐ์ ํด์ โ DOM์ ํ ๋ฒ๋ง ๋ฃ์ต๋๋ค.
- ๊ฐ๋ฐ ๋ชจ๋:
<style>
ํ๊ทธ์ ๊ทธ๋๋ก ๋ฌธ์์ด ์ฝ์ - ๋ฐฐํฌ ๋ชจ๋: ๋ ๋น ๋ฅธ
insertRule()
API ์ฌ์ฉ
- Node ํ๊ฒฝ์์ ๋ฏธ๋ฆฌ ๋ฌธ์์ด์ ๋ฝ์
<style data-emotion>
ํ๊ทธ๋ฅผ HTML์ ๋ฃ์ด ์ค๋๋ค. - ๋ธ๋ผ์ฐ์ ๋ ์ด ํ๊ทธ๋ฅผ ๋ณด๊ณ ๋ค์ ๋ ๋๋งํ์ง ์์๋ ๋ฐ๋ก ์คํ์ผ์ด ์ ์ฉ!
- Emotion ๋ด๋ถ๋ React Context(=createContext) ๋ก ํ ๋ง ๋ฑ์ ๊ด๋ฆฌํฉ๋๋ค.
-
Server Component(๊ธฐ๋ณธ) ์์์ Emotion ์ปดํฌ๋ํธ๋ฅผ ๋ฐ๋ก ์ฐ๋ฉด
โ ์๋ฒ์์createContext()
๊ฐ ํธ์ถ๋์ด ์ ์ค๋ฅ๊ฐ ๋์.
-
Provider ์ปดํฌ๋ํธ๋ฅผ ํ๋ ๋ง๋ ํ
๊ทธ ํ์ผ ๋งจ ์์'use client'
๋ฅผ ๋ถ์ธ๋ค. -
app/layout.tsx
๊ฐ์ Server Component์์๋ก ๊ฐ์ธ๋ฉด ๋!<EmotionProvider> โฆ </EmotionProvider>
์ฆ Server ์ชฝ์ HTML๋ง, Client ์ชฝ์ ์คํ์ผ & ์ํ ๊ด๋ฆฌ๋ฅผ ๋ด๋นํ๊ฒ ๋๋๋ ๊ฒ์ด ํฌ์ธํธ.
์์ | ํ ์ค ์์ฝ |
---|---|
โ ์์ฑ |
styled / css ๋ก ์คํ์ผ ์ ์ธ |
โก ์ง๋ ฌํ | ๋ฌธ์์ด + ํด์ ์์ฑ (์ค๋ณต ๋ฐฉ์ง) |
โข ์ฃผ์ | ํด๋ผ: <style> or insertRule / ์๋ฒ: <style data-emotion>
|
โฃ ์ค๋ฅ? | Server์์ Emotion์ ๋ฐ๋ก ์ฐ๋ฉด Context ์๋ฌ โ Provider + 'use client' |
- ์ ์ ์คํ์ผ์ ์ปดํฌ๋ํธ ๋ฐ ๋ณ์๋ก ์ ์ธ โ ๋ ๋๋ง๋ค ํด์ ์ฌ๊ณ์ฐ โ
- Babel ํ๋ฌ๊ทธ์ธ ์ฌ์ฉ โ ์ ์ CSS๋ฅผ ์ปดํ์ผ ํ์์ ์ฒ๋ฆฌ
- ์ ์ญ Provider ํ๋๋ง
'use client'
โ ๋ฒ๋ค ์ฌ์ด์ฆ ์ต์ํ
2025โ05โ14 ๊ธฐ์ค ยท Emotion v11 ยท Next.js 14
@emotion/style๋ฅผ ์ฌ์ฉํ๋ ๊ณณ์ 'use client'๋ฅผ ์๋จ์ ์ถ๊ฐํ์ฌ Client Component๋ก ์ง์ ํด์ฃผ๋ฉด ๋๋ค.
โstyled๋ฅผ importํ๋ค = ๊ทธ ํ์ผ์์ React Context๊ฐ ์ฆ์ ํ์ํ๋คโ
๋ฐ๋ผ์ ์๋ฒ์์ ์คํ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ ํ์ผ์ด๋ผ๋ฉด 'use client' ์์ด @emotion/styled๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋ฅ๊ฐ ๋ฉ๋๋ค.
Client ๊ฒฝ๊ณ๋ฅผ ๋ช ํํ ๋๋๊ณ ํ์ํ ๊ณณ์๋ง 'use client'๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด ๋ฒ ์คํธ
- ๊ฐ์ธ ์๊ฐ: NextJS์ Emotion์ ์๋ก ๊ทธ๋ฆฌ ์ด์ธ๋ฆฌ์ง ์์ ๊ฒ ๊ฐ๋ค..