Notice
Recent Posts
Recent Comments
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

HYEWON JUNG의 개발일지

The slice reducer for key "counter" 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 thi.. 본문

오류모음

The slice reducer for key "counter" 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 thi..

혜won 2023. 11. 28. 15:52

상황 

리액트 심화에 들어가기 전 counter앱을 기본세팅 중

 

번역

초기화 중에 키 "카운터"에 대한 슬라이스 리듀서가 정의되지 않은 상태를 반환했습니다. 리듀서에 전달된 상태가 정의되지 않은 경우 초기 상태를 명시적으로 반환해야 합니다. 초기 상태는 정의되지 않을 수 없습니다. 이 리듀서의 값을 설정하지 않으려면 정의되지 않음 대신 null을 사용할 수 있습니다.

 

 

해당코드

const counter = (state = initialState, action) => {
  switch (action.type) {
    case PLUS_ONE:
      return { number: state.number + 1 };

    case MINUS_ONE:
      return { number: state.number - 1 };

  }
};

해결코드

const counter = (state = initialState, action) => {
  switch (action.type) {
    case PLUS_ONE:
      return { number: state.number + 1 };

    case MINUS_ONE:
      return { number: state.number - 1 };

    default:
      return state;
  }
};

redux reducer에는 꼭 명시적으로 default 값이 들어가 줘야한다!