[Error] A non-serializable value was detected in an action

박준석·2024년 4월 11일

Error

목록 보기
3/10

에러발생

Redux를 사용해 프로젝트를 진행 하던 중 다음과 같은 에러가 발생했다.

A non-serializable value was detected in an action

에러원인

원인은 Redux에서 값을 주고, 받을 때 object형태의 값을 srting 형태로 변환(JSON.srtingfy)하는데, 이 상황에서 변환이 불가능한 값을 전달했다는 에러이다.

해결방안

store 파일에다가 middleware: getDefaultMiddleware => getDefaultMiddleware({ serializableCheck: false }) 이 코드를 추가하면 된다.

// 기존 코드
import { configureStore } from "@reduxjs/toolkit";
import reducer from "./reducers/reducer";

const store = configureStore({
  reducer: {
    animal: reducer,
  },
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

export default store;

    
// 해결 방안 코드
import { configureStore } from "@reduxjs/toolkit";
import reducer from "./reducers/reducer";

const store = configureStore({
  reducer: {
    animal: reducer,
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({ serializableCheck: false }),
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

export default store;
profile
안녕하세요 프론트엔드 개발자입니다. 글을 이전 중입니다.

0개의 댓글