redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

getting error: A non-serializable value was detected in an action

Open MosheAzraf opened this issue 1 year ago • 6 comments

hello, im using react with redux tkq and persist, im facing some error which im not sure what occurred it, what im trying to do, is to persist authSlice in order to keep user's data while refreshing, error:

A non-serializable value was detected in an action, in the path: `register`. Value: ƒ register2(key) {
    _pStore.dispatch({
      type: REGISTER,
      key
    });
  } 
Take a look at the logic that dispatched this action:  {type: 'persist/PERSIST', register: ƒ, rehydrate: ƒ} 
(See https://redux.js.org/faq/actions#why-should-type-be-a-string-or-at-least-serializable-why-should-my-action-types-be-constants) 
(To allow non-serializable values see: https://redux-toolkit.js.org/usage/usage-guide#working-with-non-serializable-data)

here is my code, any idea what cause it ? redux store:

import { configureStore } from "@reduxjs/toolkit";
import { moviesApi } from './services/movies/moviesApi';
import { membersApi } from "./services/members/members";
import { subscriptionsApi } from "./services/subscriptions/subscriptions";
import { usersApi } from "./services/users/usersApi";
import { authApi } from "./services/auth/authApi";
import subscriptionsSlice from "./features/subscriptionsSlice";
import authReducer from "../redux/features/authSlice"
import storage from "redux-persist/lib/storage";
import persistReducer from "redux-persist/es/persistReducer";
import persistStore from "redux-persist/es/persistStore";

const persistConfig = {
    key: "auth",
    storage: storage,
}

const persistedAuthReducer = persistReducer(persistConfig, authReducer)


export const store = configureStore({
    reducer: {
        [moviesApi.reducerPath]: moviesApi.reducer,
        [membersApi.reducerPath]: membersApi.reducer,
        [subscriptionsApi.reducerPath]: subscriptionsApi.reducer,
        subInfo : subscriptionsSlice,
        [usersApi.reducerPath]: usersApi.reducer,
        [authApi.reducerPath]: authApi.reducer,
        auth: persistedAuthReducer
    },
    
    middleware: (getDefaultMiddleware) =>
        getDefaultMiddleware().concat(
            moviesApi.middleware, 
            membersApi.middleware,
            subscriptionsApi.middleware,
            usersApi.middleware,
            authApi.middleware
        ),
});

export const persistor = persistStore(store)

how i wrap it:

import { createRoot } from 'react-dom/client';
import App from './App';
import './index.css';
import { Provider } from 'react-redux';
import { store, persistor } from './redux/store.js';
import { BrowserRouter } from 'react-router-dom';
import { PersistGate } from 'redux-persist/integration/react';

const root = createRoot(document.getElementById('root'));

root.render(
  <BrowserRouter>
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <App />
      </PersistGate>      
    </Provider>
  </BrowserRouter>
);

authSlice:

import { createSlice } from "@reduxjs/toolkit";

const initialState = { 
  user: {
    userName: "",
    userFullName: null,
    role: "",
    permissions: [],
    sessionTimeOut: null,
    token: ""
  },
  isLoading: false,
  isError: false
}

const authSlice = createSlice({
  name: 'auth',
  initialState,
  reducers: {
    setUser: (state, action) => {
      return {
        ...state,
        user: action.payload.data,
        isLoading: false,
        isError: false
      };
    },
    logout: (state) => {
      return {
        ...state,
        user: initialState.user, 
      };
    },
  }
});

export const { setUser, logout } = authSlice.actions;
export default authSlice.reducer;

authApi

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

const baseURL = 'http://localhost:8001/api/auth';

export const authApi = createApi({
  reducerPath: 'authApi',
  baseQuery: fetchBaseQuery({ baseUrl: baseURL }),
  endpoints: (builder) => ({
    login: builder.mutation({
      query: ({ userName, password }) => ({
        url: 'login',
        method: 'POST',
        body: {
          userName: userName,
          password: password
        }
      }),
      transformResponse: (resp) => resp.data
      
    }),
  }),
});

export const { useLoginMutation } = authApi;

MosheAzraf avatar Oct 30 '23 21:10 MosheAzraf

Hi, I've found your issue trying to fix the same problem. I've added the changes described here (middleware part inconfigureStore) and it helped. Hope it'll help to resolve your issue too.

viraxslot avatar Nov 08 '23 19:11 viraxslot

Hi, I've found your issue trying to fix the same problem. I've added the changes described here (middleware part inconfigureStore) and it helped. Hope it'll help to resolve your issue too.

Thanks ! Good job,it works for me!

hu-qi avatar Mar 17 '24 15:03 hu-qi

Thanks !

sourabhverma05 avatar Apr 02 '24 06:04 sourabhverma05

Thanks worked.

import { persistStore, persistReducer, FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER, } from 'redux-persist'

const store = configureStore({ reducer: persistedReducer, middleware: (getDefaultMiddleware) => getDefaultMiddleware({ serializableCheck: { ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER], }, }), })

Hi, I've found your issue trying to fix the same problem. I've added the changes described here (middleware part inconfigureStore) and it helped. Hope it'll help to resolve your issue too.

uvistix avatar Apr 14 '24 14:04 uvistix

Hi, I've found your issue trying to fix the same problem. I've added the changes described here (middleware part inconfigureStore) and it helped. Hope it'll help to resolve your issue too.

Thanks it worked for me too!

Yagth avatar May 05 '24 05:05 Yagth

it is

nrooban avatar Jun 10 '24 07:06 nrooban