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

initialState rootReducer, serialise/ desialise state maintaining types #34

Closed
beckend opened this issue Jul 3, 2016 · 5 comments
Closed

Comments

@beckend
Copy link

beckend commented Jul 3, 2016

Let's say I have a reducer with initialState:

myReducer

Immutable.fromJS({
  uniqueTodos: Immutable.Set([ 'one', 'two' ])
});

Then time to save to local store:

const storeSaveObj = store.getState().toJS();
saveToLocalStorage(JSON.stringify(storeSaveObj));

Then to restore it:

import { getStateFromLocalStorage } from './utils'; 
import { immutableRootReducer } from './rootReducer';
import { createStore } from 'redux';

const initialState = Immutable.fromJS(getStateFromLocalStorage());
const store = createStore(immutableRootReducer, initialState);

Then the reducer has been restored, but as a Immutable.List because of Immutable.fromJS:

const uniqueTodos = store.getState().getIn([ 'myReducer', 'uniqueTodos' ]);
Immutable.List.isList(uniqueTodos);
true
Immutable.Set.isSet(uniqueTodos);
false

Is there a good solution to retain the immutable types somehow?

@gajus
Copy link
Owner

gajus commented Jul 3, 2016

This question has little to do with redux-immutable itself and more with https://github.com/facebook/immutable-js/.

Regardless, it is an interesting issue, one that I did not even realise I have. : )

Quick Google search brought me to https://www.npmjs.com/package/transit-immutable-js

And thats http://blog.cognitect.com/blog/2014/7/22/transit an intro to Transit data interchange format.

This looks like a reasonable solution.

@gajus gajus closed this as completed Jul 3, 2016
@gajus
Copy link
Owner

gajus commented Jul 3, 2016

@beckend I would appreciate if you'd share your findings with others. Can you raise a PR?

@gajus gajus reopened this Jul 3, 2016
@gajus gajus changed the title initialState rootReducer initialState rootReducer, serialise/ desialise state maintaining types Jul 3, 2016
@kandros
Copy link

kandros commented Jul 7, 2016

Same issue when serializing a Immutable.Set on the server and deserialize for hydratation on client, it becomes an Immutable.List.

Ill try the provided packages

@gajus
Copy link
Owner

gajus commented Jul 7, 2016

@kandros I have suggested a fix. Is it still an issue?

@gajus
Copy link
Owner

gajus commented Jan 11, 2017

For the record, I have been using transit-immutable-js in production for a while now. There are no downsides to it, other than a relatively large bundle size addition.

@gajus gajus closed this as completed Jan 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants