All Projects → rt2zz → Redux Persist Crosstab

rt2zz / Redux Persist Crosstab

Keep redux browser tab state in sync

Programming Languages

javascript
184084 projects - #8 most used programming language

Redux Persist Crosstab

Add cross tab syncing to your redux app with 1 line. This tiny module listens to the window for redux-persist storage events. When an event occurs it will dispatch a rehydrate action.

Usage

import { createStore, compose } from 'redux'
import { persistStore, autoRehydrate } from 'redux-persist'
import crosstabSync from 'redux-persist-crosstab'
const finalCreateStore = compose(autoRehydrate())(createStore)
const store = finalCreateStore(reducer)

const persistor = persistStore(store, {})
crosstabSync(persistor)

To blacklist some portion of state, for example if you want to avoid syncing route state:

crosstabSync(persistor, {blacklist: ['routeReducerKey']})

Rehydration Merge

Redux Persist does a shallow merge of state during rehydration. This means that if state changes on two tabs simulataneously, it is possible that legitimate state will be lost in the merge. In most cases this will not be an issue. One scenario where this could happen is if both tabs are listening on a socket and they both receive a message at the same time. If you have this type of set up you will either need to blacklist the relevant reducers or implement a custom rehydration handler that takes into account the nuances of this situation.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].