michaelcontento / redux-storage

Persistence layer for redux with flexible backends
MIT License
675 stars 50 forks source link

Failed to load previous state. #165

Closed petervavro closed 8 years ago

petervavro commented 8 years ago

Hi,

"load(store)" fail to load state from memory. Does anyone have a solution, please ? Thanks

Here is my code:

import { applyMiddleware, compose, createStore } from 'redux';
import { fromJS } from 'immutable';
import createSagaMiddleware from 'redux-saga';
import createReducer from './state';
import * as storage from 'redux-storage'
import devTools from 'remote-redux-devtools';

import thunk from 'redux-thunk';
import createEngine from 'redux-storage-engine-reactnativeasyncstorage';

const engine = createEngine('xyz');

const storageMiddleware = storage.createMiddleware(engine);

const sagaMiddleware = createSagaMiddleware();

function configureStore(initialState = fromJS({})) {

  const middlewares = [
    sagaMiddleware,
    thunk,
    storageMiddleware
  ];

  const enhancers = [
    applyMiddleware(...middlewares),
  ];

  if (__DEV__) {
    enhancers.push(devTools());
  }

  const store = createStore(
    createReducer(),
    initialState,
    compose(...enhancers)
  );

  const load = storage.createLoader(engine);

  // Notice that our load function will return a promise that can also be used
  // to respond to the restore event.
  load(store)
      .then((newState) => console.log('Loaded state:', newState))
      .catch(() => console.log('Failed to load previous state'));

  // Extensions
  store.runSaga = sagaMiddleware.run;

  return store;
}

module.exports = configureStore;
petervavro commented 8 years ago

Sorry , the cause of issue was in "createReducer" function.

michaelcontento commented 8 years ago

Re-opened as @guns2410 want's to work on this project: Give him a warm welcome 👏

guns2410 commented 8 years ago

Thanks @michaelcontento

Happy to see @petervavro's issue is resolved.