codestates / codestates/Dajavas

[Error] The slice reducer for key "board" returned undefined during initialization. ~~ If you don't want to set a value for this reducer, you can use null instead of undefined

Open
#100 0 comments 0 reactions 1 assignee Claimed by @soyoung931014 View on GitHub
Error
Dominant language
JavaScript
Stars
2
Forks
3
PR merge metrics
No merged PRs in 30d

Description

## 어떤 에러인가요?
- 리듀서 함수 state의 초기값이 `undefined`이라고 뜬 오류이다. 초기값은 무조건 설정을 해주어야 한다해서 분명히 초기값을 설정해주었음에도 불구하고 `undefined`오류가 났다.

```jsx
reducer.js

const initialState = {

src:'',
fish_name:'',
size:'',
ranked:1
}

export const fishBoardReducer = (state=initialState, action) => {
switch (action.type) {
case ADD_FISH:
return {
...state,
src: action.payload.src,
fish_name: action.payload.fish_name,
size: action.payload.size
}

}
}

```

## 에러 메시지
스크린샷 2022-02-21 오후 11 30 54

## 에러 핸들링 방법
- default값을 설정해주지 않아 오류가 생긴것이었다. 이것 때문에 오늘 하루 고통스러웠는데.. 리듀서 함수의 첫번 째 인자`state = initialState` 때문에 내가 초기값을 넣어줬다고 착각을 했다. 엄밀히 말하자면 초기값을 할당해준건 맞지만 초기값이 반환된건 아니다.(하다만느낌?) 그리고 action.type이 설정되어있어서 default값을 넣어주어야겠다는 생각을 하지 못했다.
- 꼭 기억하자 초기값이라는 변수를 넣어줬다고 리듀서 함수에 초기 값 설정이 된것이 아니다. 디.폴.트 값을 넣어주어야 초기값이 설정되는것이다. 그리고 action.type이 있다해서 디폴트값을 잊으면 안된다.

```jsx
export const fishBoardReducer = (state=initialState, action) => {
switch (action.type) {
case ADD_FISH:
return {
...state,
src: action.payload.src,
fish_name: action.payload.fish_name,
size: action.payload.size
}
default: return state ⭐️
}
}
```

## 에러 핸들링을 위해 참고한 레퍼런스 링크
없음

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.