본문 바로가기

오류해결

Error: Reducer "A" returned undefined during initialization

Error: Reducer "A" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.

 

리듀서에서 default state를 반환하지 않을 때 위와 같은 에러가 발생한다.

const auth = (state: AuthState = initialState, action: AuthAction) => {
  switch (action.type) {
    case ACTION1:
    	...
    case ACTION2:
      	...
    default:
      break; // Error
  }
};

다음과 같이 default 일 때 상태를 반환해 주면 해결이 가능하다.

const auth = (state: AuthState = initialState, action: AuthAction) => {
  switch (action.type) {
    case ACTION1:
    	...
    case ACTION2:
      	...
    default:
      return state // Ok
  }
};