9a135bd19d
최상위 10_Wiki/Topic_*였던 4개 카테고리 폴더를 10_Wiki/Topics/Topic_* 로 재배치. 콘텐츠 변경 없음(순수 폴더 이동) — Topics/ 하위 나머지 폴더는 이미 지난 커밋에서 전부 정리된 상태(잔존 항목은 에이전트 운영 상태 및 사용자가 보존을 요청한 업데이트0615/무제 3.canvas 뿐).
3.4 KiB
3.4 KiB
id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
| id | title | category | status | source_trust_level | verification_status | created_at | updated_at | tags | tech_stack | applied_in | aliases | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| react-reducer-usereducer | useReducer — 복잡 상태 전이 | Coding | draft | B | conceptual | 2026-05-09 | 2026-05-09 |
|
|
|
useReducer — 복잡 상태 전이
여러 setState 호출이 한 묶음으로 일어나거나, 다음 상태가 이전 상태에 의존하면 useReducer. action 을 discriminated union 으로 정의하면 state 전이가 곧 state machine.
📖 핵심 개념
(state, action) => state순수 함수.- 호출자는 dispatch(action) — UI 와 비즈니스 로직 분리.
- 테스트 = reducer 함수만 단위 테스트.
💻 코드 패턴
type State =
| { kind: 'idle' }
| { kind: 'loading' }
| { kind: 'success'; data: User }
| { kind: 'error'; message: string };
type Action =
| { type: 'fetch_start' }
| { type: 'fetch_success'; data: User }
| { type: 'fetch_error'; message: string }
| { type: 'reset' };
function reducer(state: State, action: Action): State {
switch (action.type) {
case 'fetch_start': return { kind: 'loading' };
case 'fetch_success': return { kind: 'success', data: action.data };
case 'fetch_error': return { kind: 'error', message: action.message };
case 'reset': return { kind: 'idle' };
}
}
function User({ id }: { id: string }) {
const [state, dispatch] = useReducer(reducer, { kind: 'idle' });
useEffect(() => {
dispatch({ type: 'fetch_start' });
const ac = new AbortController();
fetch(`/api/users/${id}`, { signal: ac.signal })
.then(r => r.json())
.then(data => dispatch({ type: 'fetch_success', data }))
.catch(e => { if (e.name !== 'AbortError') dispatch({ type: 'fetch_error', message: e.message }); });
return () => ac.abort();
}, [id]);
switch (state.kind) {
case 'idle': return null;
case 'loading': return <Spinner />;
case 'success': return <Profile user={state.data} />;
case 'error': return <Error msg={state.message} />;
}
}
🤔 의사결정 기준
| 상황 | useReducer | useState |
|---|---|---|
| 상태 필드 1~2개 | ❌ | ✅ |
| 다음 상태가 여러 필드 동시 변경 | ✅ | useState 도 가능하지만 reducer 가 명확 |
| 상태 머신 (idle/loading/success) | ✅ | ❌ |
| 깊은 자식이 dispatch 만 알면 됨 | ✅ (Context 로 dispatch 만 내려) | ❌ |
| 외부 라이브러리 (Zustand) 더 나은 경우 | 글로벌 상태면 외부 store | useReducer 는 컴포넌트 단위 |
❌ 안티패턴
- action 에 함수/promise 넣기: action 은 직렬화 가능 데이터. side effect 는 effect 안에서 dispatch.
- action.type 을 string union 안 만듦: typo 가능. union 으로.
- reducer 안에서 fetch / setTimeout: pure 깨짐. effect 가 dispatch.
- state 직접 mutate (
state.foo = 1): React 가 변화 감지 못 함. 항상 새 객체 반환. - switch 에 default 없음: 새 action 추가 시 누락 모름. assertNever 패턴.
🤖 LLM 활용 힌트
- "복잡 상태 전이 → discriminated union state + action, exhaustive switch" 명시.
- Immer 사용 시 reducer 안에서 mutation OK (immer 가 immutable 변환).