├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Redux Persist Transform Immutable 2 | Add immutable sub-reducer support to redux-persist. 3 | 4 | **NOTE** this handles immutable state on a per-reducer basis. If your top level state is an immutable map, this module will not work. 5 | 6 | ## Breaking Change 7 | v5 changes from `transitjs` to `remotedev-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! 8 | 9 | ### Usage with Redux Persist 10 | ```js 11 | import { createStore, combineReducers } from 'redux' 12 | import { persistStore, persistReducer } from 'redux-persist' 13 | import immutableTransform from 'redux-persist-transform-immutable' 14 | 15 | const persistConfig = { 16 | transforms: [immutableTransform()], 17 | key: 'root', 18 | storage 19 | } 20 | 21 | const reducer = combineReducers(reducers) 22 | const persistedReducer = persistReducer(persistConfig, reducer) 23 | const store = createStore(persistedReducer) 24 | 25 | persistStore(store) 26 | ``` 27 | 28 | #### Config 29 | For config, please refer to [redux-persist's docs](https://github.com/rt2zz/redux-persist/blob/master/docs/api.md#type-persistconfig). 30 | 31 | ### Usage with Records 32 | By default, immutable [`Record`s](https://facebook.github.io/immutable-js/docs/#/Record) will be persisted and restored as `Map`s, because the library has no way of knowing what your `Record` constructor looks like. To change this behavior and allow a `Record` to be persisted and restored as a `Record` instance, you'll need to do two things: 33 | 34 | 1. Add a name attribute to your record (this is the second argument to a `Record`'s constructor). 35 | 2. Pass your `Record` constructor to the transformer's `withRecords()` function to generate a transformer capable of serializing and deserializing the record. 36 | 37 | Minimal example: 38 | ```js 39 | import { compose } from 'redux' 40 | import { persistStore, autoRehydrate } from 'redux-persist' 41 | import immutableTransform from 'redux-persist-transform-immutable' 42 | 43 | const reducer = combineReducers(reducers) 44 | const store = compose(autoRehydrate(), createStore)(reducer) 45 | 46 | const MyRecord = Record({ 47 | foo: 'null' 48 | }, 'MyRecord') // <- Be sure to add a name field to your record 49 | 50 | persistStore( 51 | store, 52 | { 53 | transforms: [immutableTransform({records: [MyRecord]})] 54 | } 55 | ) 56 | 57 | ``` 58 | 59 | ### Avoiding Unnecessary Serialization 60 | 61 | By default, `redux-persist-immutable-transform` will serialize and deserialize *all* passed objects using `transit-immutable-js`. If you are concerned about performance, you can either whitelist or blacklist reducer that you know are not immutable. 62 | 63 | Example state object: 64 | 65 | ```js 66 | state = { 67 | username: 'john', 68 | imageUri: 'images/profilePic.png', 69 | friends: Immutable.List([ ... ]) 70 | } 71 | ``` 72 | 73 | Set up the transformer to ignore the string-based reducer keys: 74 | 75 | ```js 76 | persistStore(store, { 77 | transforms: [immutableTransform({ 78 | blacklist: ['username', 'imageUri'] 79 | })] 80 | }) 81 | 82 | /* OR */ 83 | 84 | persistStore(store, { 85 | transforms: [immutableTransform({ 86 | whitelist: ['friends'] 87 | })] 88 | }) 89 | ``` 90 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Immutable = require('immutable') 2 | var Serialize = require('remotedev-serialize') 3 | var reduxPersist = require('redux-persist') 4 | 5 | module.exports = function (config) { 6 | config = config || {} 7 | 8 | var serializer = Serialize.immutable(Immutable, config.records) 9 | 10 | return reduxPersist.createTransform(serializer.stringify, serializer.parse, config) 11 | } 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-persist-transform-immutable", 3 | "version": "5.0.0", 4 | "description": "immutable.js transform for redux-persist", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"no test specified\" && exit 0" 8 | }, 9 | "dependencies": { 10 | "remotedev-serialize": "^0.1.0" 11 | }, 12 | "peerDependencies": { 13 | "redux-persist": "^4.0.0", 14 | "immutable": ">= 3" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/rt2zz/redux-persist-transform-immutable.git" 19 | }, 20 | "keywords": [ 21 | "redux", 22 | "redux-persist", 23 | "redux-persist-transform", 24 | "compression" 25 | ], 26 | "homepage": "https://github.com/rt2zz/redux-persist-transform-immutable", 27 | "author": "rt2zz ", 28 | "license": "MIT" 29 | } 30 | --------------------------------------------------------------------------------