-
Notifications
You must be signed in to change notification settings - Fork 864
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
Any way to use redux-persist with immutable Map as the entire state? #64
Comments
I am interested in supporting top level immutable state. You can I believe this will require a new option After typing this out, I believe it is possible to implement with no impact on performance, but it will take some work so no ETA at this time, although PR's are appreciated. If you are short on time I would probably recommend looking at https://github.com/michaelcontento/redux-storage which deals which avoids dealing with state partials and hence has a much simpler story around immutable support. |
If we omit autorehydrate, it should be easy implemented with Alternate Usage getStoredState(persistConfig, (err, initialState) => {
const store = createStore(reducer, Immutable.fromJS(initialState))
persistStore(store, persistConfig)
// ...
}) |
that works very well for rehydration, but there is still an outstanding problem where persistStore expects the state object returned from getState to be a plain object. A trivial albeit non-ideal performance solution would be to call |
I guess if we do |
I see, not entirely related to this issue but does using an immutable structure as the top level state provide any value? |
@peterlazar1993, I think no signifiant value. Better to use the standard Redux Then we'll have something like getStoredState(persistConfig, (err, initialState) => {
const initialImmutableState = {};
Object.keys(initialState).forEach(function(key) {
initialImmutableState[key] = fromJS(obj[key]);
});
const store = createStore(reducer, initialImmutableState)
persistStore(store, persistConfig)
// ...
}) @rt2zz, I guess in this case we don't need any changes in |
connect from react-redux implements a shouldComponentUpdate, I think there might be some value in having a top level immutable state. For now, I've decided to use Immutable structures on a per reducer basis. |
@peterlazar1993, it shouldn't matter there even if we don't use |
@zalmoxisus Sure. Thank you :) |
@zalmoxisus interesting to hear you are moving your sub reducers to immutable, what spurred this change? Is it a performance optimization or that the api is preferable? Yes if you go with the approach in #64 (comment) that example should work without modification. I agree with @zalmoxisus that in a typical redux app there is no benefit to top level immutable state. Keeping things an object at the top level helps with interoperability. An alternative and simpler option to the getStoredState approach is to use https://github.com/rt2zz/redux-persist-immutable. Which approach is best will be application specific. |
@rt2zz I've used https://github.com/rt2zz/redux-persist-immutable, neat little library :) |
@rt2zz, you remember I was against it before :) |
Just an update, in case someone will use the snippet I provided above. Since import { fromJS } from 'immutable';
import { getStoredState, persistStore } from 'redux-persist';
const whitelist = ['reducerName'];
const persistConfig = {
skipRestore: true,
whitelist
};
getStoredState(persistConfig, (err, initialState) => {
const initialImmutableState = {};
whitelist.forEach((key) => {
if (initialState[key]) initialImmutableState[key] = fromJS(initialState[key]);
});
const store = createStore(reducer, initialImmutableState)
persistStore(store, persistConfig)
// ...
}); |
https://github.com/rufman/redux-persist/tree/feature/hacked-immutable adds a config option to deal with any random kind of root state. I use it to shallow convert an ImmutableJS state i.e. store.getState().toObject(), so that the root state is a JS object that the rest of the library can deal with. The subStates are still Immutable objects, so I use |
@rufman, wouldn't this shallow convert affect the performance? I still don't get what could be the advantage of having the root state in ImmutableJS. |
It's not as bad as doing |
in the forthcoming v3 release the createPersistor and getStoredState methods are sufficiently abstracted that I believe we could easily create a The following extra config/extension will be needed: In order to accept these changes in redux-persist however we would need some benchmarks to ensure we are not introducing performance regressions. |
@rt2zz I'll look into creating an immutable module and doing some benchmark testing, when I get some free time. |
I've pushed an initial implementation for this issue here: https://github.com/rufman/redux-persist/tree/master any feedback would be nice. I'll open a PR one I've created benchmark tests. https://www.npmjs.com/package/redux-persist-immutable-state is a new npm-module I published that implements all the config hooks added above for ImmutableJS. Usage:
|
Would be really nice if the library supported to-level immutable object. This is being used in mxstbr/react-boilerplate, which is one of the most popular boilerplates in React community today. What are the current plans on resolving this issue? |
Ah, I wish I saw your PR this morning! Ended up with some clumsy solution based on the original library instead and spent so much time on this! I'll try your modified version of the module in the next project and actually hope that your PR will be finally merged by then! |
Ok tentative immutable support now available at https://github.com/rt2zz/redux-persist-immutable note I have not actually run this in an app, but the tests pass ;) cc/ @rufman if you have a moment to check out my PR on your repo, it would be great to make that the main repo and get you added to the npm module as well |
@rt2zz I'm planning on doing that this weekend |
@rt2zz why have you closed this issue? Has there been a PR that solves it? |
tentative support via https://github.com/rt2zz/redux-persist-immutable v4.0.0-alpha3 redux-persist-immutable is a drop in replacement for redux-persist that has immutable support |
Want to integrate redux-persist with this https://github.com/multunus/react-native-boilerplate. Maybe a recipe for immutable state
The text was updated successfully, but these errors were encountered: