@@ -2,14 +2,14 @@ import { useEffect } from "react";
22import { Button } from "./components/ui/button" ;
33import {
44 CounterQueryProvider ,
5- setCounterState ,
6- updateCounterState ,
75 useCounterQuery ,
6+ useCounterSetter ,
87} from "./CounterContextQueryProvider" ;
98import { cqLogger } from "./LoggerInstance" ;
109
1110function CQCounter3 ( ) {
12- const [ { count3 } , setState ] = useCounterQuery ( [ "count3" ] ) ;
11+ const [ { count3 } ] = useCounterQuery ( [ "count3" ] ) ;
12+ const setState = useCounterSetter ( ) ;
1313
1414 useEffect ( ( ) => {
1515 cqLogger . log ( `Counter3 컴포넌트 렌더링 - ${ count3 } ` ) ;
@@ -24,7 +24,7 @@ function CQCounter3() {
2424 } ;
2525
2626 const reset = ( ) => {
27- setState ( { count3 : 0 } ) ;
27+ setState ( ( prev ) => ( { ... prev , count3 : 0 } ) ) ;
2828 } ;
2929
3030 return (
@@ -51,22 +51,27 @@ function CQCounter3() {
5151}
5252
5353function CQCounter2 ( ) {
54- const [ { count2 } , setState ] = useCounterQuery ( [ "count2" ] ) ;
54+ const [ { count2 } ] = useCounterQuery ( [ "count2" ] ) ;
55+ const setState = useCounterSetter ( ) ;
5556
5657 useEffect ( ( ) => {
5758 cqLogger . log ( `Counter2 컴포넌트 렌더링 - ${ count2 } ` ) ;
5859 } ) ;
5960
6061 const increment = ( ) => {
61- setState ( ( prev ) => ( { ...prev , count2 : prev . count2 + 1 } ) ) ;
62+ setState ( ( prev ) => ( {
63+ ...prev ,
64+ count2 : prev . count2 + 1 ,
65+ count3 : prev . count3 + 1
66+ } ) ) ;
6267 } ;
6368
6469 const decrement = ( ) => {
6570 setState ( ( prev ) => ( { ...prev , count2 : prev . count2 - 1 } ) ) ;
6671 } ;
6772
6873 const reset = ( ) => {
69- setState ( { count2 : 0 } ) ;
74+ setState ( ( prev ) => ( { ... prev , count2 : 0 } ) ) ;
7075 } ;
7176
7277 return (
@@ -95,22 +100,39 @@ function CQCounter2() {
95100}
96101
97102const CQCounter1 = ( ) => {
98- const [ state , setState ] = useCounterQuery ( [ "count1" , "count2" ] ) ;
103+ // count1만 구독하여 리렌더링 대상으로 설정
104+ const [ { count1 } ] = useCounterQuery ( [ "count1" ] ) ;
105+
106+ // 전체 상태 업데이트용 setter
107+ const setState = useCounterSetter ( ) ;
99108
100109 useEffect ( ( ) => {
101- cqLogger . log ( `Counter1 컴포넌트 렌더링 - ${ state . count1 } ${ state . count2 } ` ) ;
110+ cqLogger . log ( `Counter1 컴포넌트 렌더링 - ${ count1 } ` ) ;
102111 } ) ;
103112
104113 const increment = ( ) => {
105- setState ( ( prev ) => ( { count1 : prev . count1 + 1 , count2 : prev . count2 + 1 } ) ) ;
114+ // 전체 상태를 한번에 업데이트
115+ setState ( ( prev ) => ( {
116+ count1 : prev . count1 + 1 ,
117+ count2 : prev . count2 + 1 ,
118+ count3 : prev . count3 + 1
119+ } ) ) ;
106120 } ;
107121
108122 const decrement = ( ) => {
109- setState ( ( prev ) => ( { count1 : prev . count1 - 1 , count2 : prev . count2 - 1 } ) ) ;
123+ setState ( ( prev ) => ( {
124+ count1 : prev . count1 - 1 ,
125+ count2 : prev . count2 - 1 ,
126+ count3 : prev . count3 - 1
127+ } ) ) ;
110128 } ;
111129
112130 const reset = ( ) => {
113- setState ( { count1 : 0 , count2 : 0 } ) ;
131+ setState ( {
132+ count1 : 0 ,
133+ count2 : 0 ,
134+ count3 : 0
135+ } ) ;
114136 } ;
115137
116138 return (
@@ -120,7 +142,7 @@ const CQCounter1 = () => {
120142 < h2 className = "text-lg font-semibold" > 카운터 1</ h2 >
121143 < div className = "flex items-center justify-between gap-4" >
122144 < span className = "text-2xl font-bold text-blue-600" >
123- { state . count1 } , { state . count2 }
145+ { count1 }
124146 </ span >
125147 < div className = "flex gap-2" >
126148 < Button size = "sm" variant = "outline" onClick = { decrement } >
@@ -141,35 +163,9 @@ const CQCounter1 = () => {
141163} ;
142164
143165export function ContextQueryImplementation ( ) {
144- const incrementCount1 = ( ) => {
145- setCounterState ( "count1" , ( prev ) => prev + 1 ) ;
146- } ;
147-
148- const incrementCount2 = ( ) => {
149- setCounterState ( "count2" , ( prev ) => prev + 1 ) ;
150- } ;
151-
152- const incrementCount3 = ( ) => {
153- setCounterState ( "count3" , ( prev ) => prev + 1 ) ;
154- } ;
155-
156- const incrementAllCounts = ( ) => {
157- updateCounterState ( ( state ) => ( {
158- count1 : state . count1 + 1 ,
159- count2 : state . count2 + 1 ,
160- count3 : state . count3 + 1 ,
161- } ) ) ;
162- } ;
163-
164166 return (
165167 < div >
166- < div className = "flex justify-center space-x-4 mt-4 mb-4" >
167- < Button onClick = { incrementCount1 } > count1 증가</ Button >
168- < Button onClick = { incrementCount2 } > count2 증가</ Button >
169- < Button onClick = { incrementCount3 } > count3 증가</ Button >
170- < Button onClick = { incrementAllCounts } > 전체 증가</ Button >
171- </ div >
172- < CounterQueryProvider >
168+ < CounterQueryProvider initialState = { { count1 : 0 , count2 : 0 , count3 : 0 } } >
173169 < div className = "rounded-lg bg-card text-card-foreground shadow-sm p-4" >
174170 < h2 className = "text-xl font-bold mb-2" > Context Query 버전</ h2 >
175171 < p className = "text-muted-foreground mb-4" >
@@ -180,91 +176,9 @@ export function ContextQueryImplementation() {
180176 < CQCounter1 />
181177 < CQCounter2 />
182178 < CQCounter3 />
183- < AllStateSub />
184179 </ div >
185-
186- < BatchCounterControls />
187180 </ div >
188181 </ CounterQueryProvider >
189182 </ div >
190183 ) ;
191184}
192-
193- // 새로운 컴포넌트 추가: 일괄 처리 컨트롤
194- function BatchCounterControls ( ) {
195- useEffect ( ( ) => {
196- cqLogger . log ( "BatchCounterControls 컴포넌트 렌더링" ) ;
197- } ) ;
198-
199- const incrementAll = ( ) => {
200- updateCounterState ( ( state ) => ( {
201- count1 : state . count1 + 1 ,
202- count2 : state . count2 + 1 ,
203- count3 : state . count3 + 1 ,
204- } ) ) ;
205- } ;
206-
207- const decrementAll = ( ) => {
208- updateCounterState ( ( state ) => ( {
209- count1 : state . count1 - 1 ,
210- count2 : state . count2 - 1 ,
211- count3 : state . count3 - 1 ,
212- } ) ) ;
213- } ;
214-
215- const resetAll = ( ) => {
216- updateCounterState ( ( ) => ( {
217- count1 : 0 ,
218- count2 : 0 ,
219- count3 : 0 ,
220- } ) ) ;
221- } ;
222-
223- const multiplyAll = ( ) => {
224- updateCounterState ( ( state ) => ( {
225- count1 : state . count1 * 2 ,
226- count2 : state . count2 * 2 ,
227- count3 : state . count3 * 2 ,
228- } ) ) ;
229- } ;
230-
231- return (
232- < div className = "rounded-lg bg-card text-card-foreground shadow-sm mt-6 p-4 border-2 border-dashed border-purple-300" >
233- < h2 className = "text-lg font-semibold mb-3" > 일괄 처리 컨트롤</ h2 >
234- < p className = "text-sm text-muted-foreground mb-4" >
235- 모든 카운터를 한 번에 업데이트합니다
236- </ p >
237- < div className = "flex gap-2 flex-wrap" >
238- < Button onClick = { incrementAll } variant = "default" >
239- 모두 증가
240- </ Button >
241- < Button onClick = { decrementAll } variant = "outline" >
242- 모두 감소
243- </ Button >
244- < Button onClick = { resetAll } variant = "secondary" >
245- 모두 초기화
246- </ Button >
247- < Button onClick = { multiplyAll } variant = "destructive" >
248- 모두 2배
249- </ Button >
250- </ div >
251- </ div >
252- ) ;
253- }
254-
255- function AllStateSub ( ) {
256- const [ state ] = useCounterQuery ( ) ;
257-
258- useEffect ( ( ) => {
259- cqLogger . log ( "AllStateSub 컴포넌트 렌더링" ) ;
260- } ) ;
261-
262- return (
263- < div >
264- < h2 > AllStateSub</ h2 >
265- < p > { state . count1 } </ p >
266- < p > { state . count2 } </ p >
267- < p > { state . count3 } </ p >
268- </ div >
269- ) ;
270- }
0 commit comments