createReducer()
리듀서를 작성하다보면 switch 문으로 작성하게 되는데 createRedicer()
는 switch문 보다 더 단순한 구조로 리듀서를 작성하도록 도와주는 함수다.
// 액션 타입 불러오기 or createAction()로 액션생성
const CREATE = createAction('user/CREATE')
const UPDATE = createAction('user/UPDATE')
const DELETE = createAction('user/DELETE')
const DELETE_ALL = createAction('user/DELETE_ALL')
const reducer = createReducer(initState, {
[CREAT]: (state, action) => state.users.push(action.payload),
[UPDATE]: (state, action) =>
(users.users = users.users.filter(
(user) =>
(user.id === action.payload.id && user.name = action.payload.name)
)),
[DELETE]: (state, action) =>
(users.users = users.users.filter((user) => user.id !== action.payload)),
[DELETE_ALL]: (state) => (state.users = []),
});