psperber / redux-persist-electron-storage

Redux persist adapter for electron-store
52 stars 5 forks source link

Redux is not persisting in electron #17

Open defi-bear opened 3 years ago

defi-bear commented 3 years ago
import { combineReducers } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

import configureStore from './config';
import coreReducer from './core/reducers';

const createElectronStorage = window.require("redux-persist-electron-storage");
const ElectronStore = window.require('electron-store');
// console.log('store:', window.api.store)

const electronStore = new ElectronStore();

const rootPersistConfig = {
  key: 'root',
  storage: createElectronStorage({electronStore}),
  whitelist: [],
};

const corePersistConfig = {
  key: 'core',
  storage: createElectronStorage({electronStore}),
  whitelist: [],
};

const rootReducer = persistReducer(
  rootPersistConfig,
  combineReducers({
    core: persistReducer(corePersistConfig, coreReducer),
    // auth: persistReducer(authPersistConfig, authReducer),
  }),
);

const initialState = window.initialReduxState;
const { store } = configureStore(initialState, rootReducer);

const persistor = persistStore(store);

export {
  store,
  persistor,
};

This is my code configuring the redux persist. I tried but it's not persist redux if I reload the app.

9oelM commented 3 years ago

I experienced the similar issue. The symptom seems to be just that it's not calling .persist() for some reason. What I did was just:

// For some reason, redux-persist works this way on Electron
if (isElectron()) {
  persistor.persist();
}

Hope it helps. Wasted like a day on this.

You can prove that .persist() is not being called by putting a component into loading prop in PersistGate. The loading never stops.