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

Record rehydrating as a map

Open eamonpenland opened this issue 8 years ago • 11 comments

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?

eamonpenland avatar Mar 08 '17 23:03 eamonpenland

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!

rt2zz avatar Mar 15 '17 05:03 rt2zz

We have a similar issue where our Immutable.List is coming back as a Immutable.Map using [email protected] and [email protected]. This may be upstream and related to this issue. We were able to solve this for now by rolling back to [email protected] and to [email protected]. This also only seems to happen when using AsyncStorage with react-native.

rynbyjn avatar Apr 07 '17 20:04 rynbyjn

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

ndbroadbent avatar Apr 09 '17 16:04 ndbroadbent

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

rt2zz avatar Apr 14 '17 02:04 rt2zz

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

rt2zz avatar Apr 14 '17 02:04 rt2zz

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 avatar Apr 16 '17 16:04 ylg

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?

ylg avatar Apr 26 '17 22:04 ylg

+1

cbjs avatar May 02 '17 08:05 cbjs

+1

formatlos avatar May 02 '17 08:05 formatlos

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

rt2zz avatar May 04 '17 18:05 rt2zz

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

peterorum avatar May 24 '17 05:05 peterorum