Skip to content

Commit 2e93b35

Browse files
committed
docs: 뭐? Context 가 복사가 된다고? (Context Provider Hell 해결하기)
1 parent cc59cd8 commit 2e93b35

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: 뭐? Context 가 복사가 된다고? (Context Provider Hell 해결하기)
3+
createdAt: 2025-07-23
4+
category: React
5+
description: React Context API를 사용할 때 발생할 수 있는 'Context Provider Hell'이 무엇인지, 그리고 이를 해결하기 위해 접근한 방법에 대해 설명합니다.
6+
comment: true
7+
---
8+
9+
# 뭐? Context 가 복사가 된다고? (feat. Context Provider Hell)
10+
11+
![Prop Drilling](./img/context-provider-hell/prop-drilling.png)
12+
13+
React 를 사용하다 보면 전역적인 상태나 설정을 공유하기 위해 자연스럽게 Context를 사용하게 됩니다. 테마, 인증정보, 폼 상태 ... 처음엔 편합니다. `<Provider/>` 로 감싸주면, 어디서든 데이터를 쉽게 꺼내쓸 수 있으니까요.
14+
15+
하지만 프로젝트가 조금만 커지면 상황이 달라집니다. 컴포넌트 트리의 최상단에 `<Provider/>` 들이 복사가 되기 시작합니다. 🤮 <br />
16+
17+
저는 NextJS Page Router 에서 각각의 퍼널에 `getServerSideProps` 로 부터 받은 데이터를 Context 로 Funnel에 내려주는 작업을 하다가 이 문제를 겪었습니다.
18+
19+
```tsx
20+
export const getServerSideProps = async (context) => {
21+
// ... fetch data
22+
};
23+
24+
export default function ApplyExamPage() {
25+
return (
26+
{/* [!code focus] */}
27+
<OptionalFormProvider {...methods}>
28+
{/* [!code focus] */}
29+
<ExamAreaProvider examAreas={examAreas}>
30+
{/* [!code focus] */}
31+
<ProfileContextProvider profileData={profileData}>
32+
{/* [!code focus] */}
33+
<VerifyRegisterRecommenderContextProvider
34+
verifyRecommender={verifyCanRegisterRecommender}
35+
>
36+
<FormPersistor storageKey="mosu:form" type="sessionStorage" />
37+
38+
<Title>모의수능 신청</Title>
39+
<Funnel>
40+
<Step name="apply-area">
41+
<SiteMetadata
42+
title="신청하기 – 고사장 선택 – 모수"
43+
content="희망 시험일과 고사장을 선택하는 단계입니다. 지역별 학교 선택 가능."
44+
/>
45+
<ApplyAreaStep />
46+
</Step>
47+
{/* ... */}
48+
</Funnel>
49+
{/* [!code focus] */}
50+
</VerifyRegisterRecommenderContextProvider>
51+
{/* [!code focus] */}
52+
</ProfileContextProvider>
53+
{/* [!code focus] */}
54+
</ExamAreaProvider>
55+
{/* [!code focus] */}
56+
</OptionalFormProvider>
57+
);
58+
}
59+
```

0 commit comments

Comments
 (0)