Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

immutable support for redux-persist

NotificationsYou must be signed in to change notification settings

rt2zz/redux-persist-transform-immutable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 

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.

Breaking Change

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!

Usage with Redux Persist

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)

Config

For config, please refer toredux-persist's docs.

Usage with Records

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:

  1. Add a name attribute to your record (this is the second argument to aRecord's constructor).
  2. Pass yourRecord constructor 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]})]})

Avoiding Unnecessary Serialization

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

Stars

Watchers

Forks

Packages

No packages published

Contributors10


[8]ページ先頭

©2009-2025 Movatter.jp