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

Immutable with Nested

Open djeeg opened this issue 7 years ago • 3 comments

Hi,

I'm trying to use both immutable state and nested persists (for code splitting) https://github.com/rt2zz/redux-persist#nested-persists https://github.com/rt2zz/redux-persist-transform-immutable

Config looks like this:

const rootPersistConfig = {
  key: 'root',
  storage: storage,
  whitelist: []
}
const content1PersistConfig = {
  key: 'content1',
  storage: storage1,
  transforms: [immutableTransform({records: [Content1Record]})],
}
const content2PersistConfig = {
  key: 'content2',
  storage: storage2,
  transforms: [immutableTransform({records: [Content2Record]})],
}

const rootReducer = combineReducers({
  content1: persistReducer(content1PersistConfig, content1Reducer),
  content2: persistReducer(content2PersistConfig, content2Reducer),
})
export default persistReducer(rootPersistConfig, rootReducer)

State tree looks something like this

var rootstate = {
	content1: Content1Record{
		variable1,
		variable2,
	},
	content2: Content2Record{
		variable3,
		variable4,
	},
}

As per the README, top level state is not an ImmutableMap/Record (its plain object)

What I am finding, with default state, the Immutable Record is getting corrupted during the first PERSIST action dispatch It looks like it is this line https://github.com/rt2zz/redux-persist/blob/master/src/persistReducer.js#L66

Starts off as a healthy Immutable Record

_ref => Content1Record { variable1: false, variable2: "detail" }

Out comes an invalid object (looks like an Immutable List)

restState => { __ownerID: undefined, _values: List [ false, "detail" ] }

djeeg avatar Apr 04 '18 00:04 djeeg

My workaround is to wrap any persistReducer reducers with combineReducers

const rootReducer = combineReducers({
  [persistcontent1]: persistReducer(content1PersistConfig, combineReducers({content1: content1Reducer]}),
  [persistcontent2]: persistReducer(content2PersistConfig, combineReducers({content2: content2Reducer]}),
})

And a minor tweak to a few state selectors

export const getState = rootstate => {
    if (rootstate) {
        //let state = rootstate[stateKey];
		let state = rootstate[persist + stateKey][stateKey];
        return state;
    }
    return getInitialState();
};

Maybe this

If your top level state is an immutable map, this module will not work.

Should be changed to

The top level state or state directly under a persistReducer must not be immutable map

djeeg avatar Apr 04 '18 00:04 djeeg

Just ran into the same issue. Is this the only solution to this?

blumendorf avatar Jan 14 '19 15:01 blumendorf

@blumendorf I wrote something on the weekends to support top-level-immutable - hope it can be helpful :)

OneStromberg avatar Jan 29 '19 07:01 OneStromberg