rt2zz / redux-persist-transform-immutable

immutable support for redux-persist
112 stars 42 forks source link

Record rehydrating as a map #21

Open eamonpenland opened 7 years ago

eamonpenland commented 7 years ago

Hey having trouble to get my immutable records to rehydrate as a Record. Here is my code.

import {AsyncStorage} from 'react-native';
import createSagaMiddleware from 'redux-saga';
import devTools from 'remote-redux-devtools';
import {createStore, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import {persistStore, autoRehydrate} from 'redux-persist';
import reducer from './reducers';
import promise from './promise';
import rootSaga from 'Sagas';
import immutableTransform from 'redux-persist-transform-immutable'
import { Record } from 'immutable';

const sagaMiddleware = createSagaMiddleware()

const logger = store => next => action => {
  console.log('dispatching', action)
  let result = next(action)
  console.log('next state', store.getState())
  return result
}

const settings = Record({
    timeZoneAuto: false,
    tzName: '',
    defaultDisplay: '',
}, 'settings');

export default configureStore = (onCompletion) => {
  const enhancer = compose(applyMiddleware(promise, sagaMiddleware, logger), devTools({name: 'Supervisor', realtime: true}));

  const store = createStore(reducer, enhancer, autoRehydrate({log: true}));

  persistStore(
      store,
      {
          storage: AsyncStorage,
          transforms: [immutableTransform({records: [settings]})]
      },
      onCompletion
  );

  store.runSaga = sagaMiddleware.run(rootSaga);

  return store;
}

Any ideas?

rt2zz commented 7 years ago

code superficially looks correct. I apologize I am not sure beyond that and as I am not currently using immutable it is hard for me to take a look. If you figure the issue out please report back!

rynbyjn commented 7 years ago

We have a similar issue where our Immutable.List is coming back as a Immutable.Map using redux-persist@4.6.0 and redux-persist-transform-immutable@4.2.0. This may be upstream and related to this issue. We were able to solve this for now by rolling back to redux-persist@4.0.0 and to redux-persist-transform-immutable@4.1.1. This also only seems to happen when using AsyncStorage with react-native.

ndbroadbent commented 7 years ago

@rynbyjn I just noticed this was happening for me too. Thanks for the tip!

rt2zz commented 7 years ago

that is unfortunate. @rynbyjn what do you recommend for solving this at the lib level?

rt2zz commented 7 years ago

seems this might best be solved by making immutablejs a peer dep..

ylg commented 7 years ago

It's early days, but for the moment I was able to resolve this in our apps by requiring react-redux >= 5.0.4.

ylg commented 7 years ago

React-redux 5.0.4 was a red herring for us. At the moment, I'm not aware of any version combination that works reliably with deep trees of mixed Immutables, e.g., Map.Map.OrderedSet.Map. Rehydration always breaks something somewhere. 4.6.0 + 4.2.0 doesn't seem to help either, although it's hard to say because an area that rehydrates incorrectly one day, will seemingly work the next.

Any alternatives or workarounds others have found?

cbjs commented 7 years ago

+1

formatlos commented 7 years ago

+1

rt2zz commented 7 years ago

just published 4.3.0 with immutable as a peer dep and updated transit deps. Hopefully this helps the situation.

peterorum commented 7 years ago

No - List is still rehydrating as a Map with v4.3.0.