Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutable with Nested #34

Open
djeeg opened this issue Apr 4, 2018 · 3 comments
Open

Immutable with Nested #34

djeeg opened this issue Apr 4, 2018 · 3 comments

Comments

@djeeg
Copy link

djeeg commented Apr 4, 2018

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
Copy link
Author

djeeg commented Apr 4, 2018

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

@blumendorf
Copy link

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

@OneStromberg
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants