- Notifications
You must be signed in to change notification settings - Fork43
rt2zz/redux-persist-transform-immutable
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Add immutable sub-reducer support to redux-persist.
NOTE this handles immutable state on a per-reducer basis. If your top level state is an immutable map, this module will not work.
v5 changes fromtransitjs toremotedev-serialize. For existing projects that upgrade to v5, all persisted data will be lost upon the initial persist.note It is possible to write an upgrade via a custom transform that supports both formats - if you do write one please PR!
import{createStore,combineReducers}from'redux'import{persistStore,persistReducer}from'redux-persist'importimmutableTransformfrom'redux-persist-transform-immutable'constpersistConfig={transforms:[immutableTransform()],key:'root', storage}constreducer=combineReducers(reducers)constpersistedReducer=persistReducer(persistConfig,reducer)conststore=createStore(persistedReducer)persistStore(store)
For config, please refer toredux-persist's docs.
By default, immutableRecords will be persisted and restored asMaps, because the library has no way of knowing what yourRecord constructor looks like. To change this behavior and allow aRecord to be persisted and restored as aRecord instance, you'll need to do two things:
- Add a name attribute to your record (this is the second argument to a
Record's constructor). - Pass your
Recordconstructor to the transformer'swithRecords()function to generate a transformer capable of serializing and deserializing the record.
Minimal example:
import{compose}from'redux'import{persistStore,autoRehydrate}from'redux-persist'importimmutableTransformfrom'redux-persist-transform-immutable'constreducer=combineReducers(reducers)conststore=compose(autoRehydrate(),createStore)(reducer)constMyRecord=Record({foo:'null'},'MyRecord')// <- Be sure to add a name field to your recordpersistStore(store,{transforms:[immutableTransform({records:[MyRecord]})]})
By default,redux-persist-immutable-transform will serialize and deserializeall passed objects usingtransit-immutable-js. If you are concerned about performance, you can either whitelist or blacklist reducer that you know are not immutable.
Example state object:
state={username:'john',imageUri:'images/profilePic.png',friends:Immutable.List([ ...])}
Set up the transformer to ignore the string-based reducer keys:
persistStore(store,{transforms:[immutableTransform({blacklist:['username','imageUri']})]})/* OR */persistStore(store,{transforms:[immutableTransform({whitelist:['friends']})]})
About
immutable support for redux-persist
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.