인프런 커뮤니티 질문&답변
combineReducers사용할때 오류가 생겨요
작성
·
539
0
에러는 TypeError: Cannot read properties of undefined (reading 'type') 이렇게 출력이 되고
해당 에러가 난 코드는 reducer/user.js의
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'LOG_IN':
이부분 입니다
user.js, post.js에
if (action === undefined){return state}
를 추가해서 수정하려했지만
TypeError: Cannot read properties of undefined (reading 'isLoggedIn')
이렇게 추가로 오류가 발생합니다
어떻게 수정해야할까요..ㅜㅜ
퀴즈
리덕스의 주요 목적은 무엇인가요?
React 컴포넌트 라이프사이클 관리
서버와의 비동기 통신 최적화
애플리케이션 상태 중앙 집중 관리
UI 렌더링 성능 향상
답변 3
0
combineReducers 코드 입니다
강사님과는 다르게 index부분이 회색으로 주석 쳐진것처럼 나와요
import {HYDRATE} from "next-redux-wrapper";
import {combineReducers} from "redux";
import user from "./user";
import post from "./post";
const rootReducer = combineReducers({
index: (state = {},action) =>{
switch (action.type){
case HYDRATE:{
return {...state,...action.payload}
}
default:{
return state
}
}
},
user,
post,
})
export default rootReducer
https://github.com/ZeroCho/react-nodebird/tree/master/ch3/front
이것과 비교해보셔야 할 것 같습니다. 제 생각에는 pages쪽에서도 호출을 잘못했거나 하는 경우가 있을 수 있습니다.
0
제가 강사님 답변을 정확하게 이해하지 못해서 로그인 관련 코드를 올려보겠습니다
LoginForm.js 코드
const LoginForm = () =>{
const dispatch = useDispatch()
const [id, onChangeId] = useInput('')
const [pw, onChangePw] = useInput('')
const onSubmitForm = useCallback(() => {
console.log(id,pw)
dispatch(LoginAction({id,pw}))
},[id,pw])
----------------------------------------------------
user.js 코드
export const LoginAction = (data) => {
return{
type:'LOG_IN',
data:data,
}
}
const reducer = (state = initialState, action) => {
switch (action.type){
case 'LOG_IN':{
return{
...state,
isLoggedIn: true,
user:action.data
}
}
이렇게 작성하였는데 강의를 돌려봐도 어디가 문제인지 잘 모르겠네요..
0
action이 undefined가 되는 것 자체가 문제인데요. 혹시 dispatch()만 하신 것 아닌가요? log_in 관련 코드쪽에서 찾아보셔야 합니다.





combineReducers 코드도 올려주세요.