3-3-리듀서-리팩토링하기
초기 리듀서
const initialState = {
visibilityFilter : 'SHOW_ALL',
todos : []
};
function appReducer(state = initialState, action) {
switch(action.type) {
case 'SET_VISIBILITY_FILTER' : {
return Object.assign({}, state, {
visibilityFilter : action.filter
});
}
case 'ADD_TODO' : {
return Object.assign({}, state, {
todos : state.todos.concat({
id: action.id,
text: action.text,
completed: false
})
});
}
case 'TOGGLE_TODO' : {
return Object.assign({}, state, {
todos : state.todos.map(todo => {
if (todo.id !== action.id) {
return todo;
}
return Object.assign({}, todo, {
completed : !todo.completed
})
})
});
}
case 'EDIT_TODO' : {
return Object.assign({}, state, {
todos : state.todos.map(todo => {
if (todo.id !== action.id) {
return todo;
}
return Object.assign({}, todo, {
text : action.text
})
})
});
}
default : return state;
}
}유틸리티 함수 추출하기
케이스 리듀서 추출
케이스 리듀서 추출
데이터 관리 도메인으로 분리하기
보일러 플레이트 줄이기
슬라이스로 리듀서 결합하기
Last updated
Was this helpful?