React의 데이터 모델에서 setState, redux를 사용할 때 기본적으로 불변성을 지켜야 한다. 간단한 클릭 카운터 예제를 통해 확인해 보자.
// bad
clickHandler = () => this.state.count++;
// good
clickHandler = () => this.setState(state => ({
count: state.count + 1
}));
위의 good 케이스는 immer의 produce 사용법과 완벽히 부합한다.
// immer
const nextState = produce(currentState, draft => {
draft.count = draft.count + 1
})