combineReducer 이렇게도 할 수 있다.

김민석·2021년 6월 23일
0

Redux

목록 보기
5/5

const rootReducer = combineReducers({
  index: (state = {}, action) => {
    switch (action.type) {
      case HYDRATE:
        return { ...state, ...action.payload };
      default:
        return state;
    }
  },
  user,
  post,
});

//위의 코드를 아래처럼 바꿀 수 있다.

const rootReducer = (state, action) => {
  switch (action.type) {
    case HYDRATE : 
      return action.payload;
    default: {
      const combinedReducer = combineReducers({
        user,
        post,
      });
      return combinedReducer(state, action);
    }
  }
};

이렇게 작동하던 것이

이렇게 바뀐다. (잘 바뀐 것이다.)

0개의 댓글