reducer state machine

useReducer는 상태 전이를 명시적으로 모을 때 빛난다

상태가 여러 갈래로 바뀌고 액션 의미가 중요해질수록 reducer는 UI 이벤트와 상태 계산을 분리해 준다.

state

상태 모델

loading, success, error처럼 동시에 성립하면 안 되는 상태를 한 모델로 묶는다.

single model
action

액션 이름

버튼 클릭이 아니라 사용자의 의도를 action type으로 표현한다.

intent event
pure

순수 reducer

현재 state와 action만으로 다음 state를 계산하고 외부 작업은 넣지 않는다.

predictable
dispatch

이벤트 연결

컴포넌트는 계산을 직접 하지 않고 적절한 action을 dispatch한다.

thin UI
test

전이 테스트

state와 action을 넣어 expected state가 나오는지 함수 단위로 검증한다.

unit proof