A store enhancer for Redux which allows to mirror one store as a part of another and keeps them in sync.
Although one of the core principles of Redux is referring to a single source of truth, depending on the application its not always possible. Environments like iframes
or Web Worker
can't share code directly as they are running in a different context. While its possible to use just one store and pass each data manually, another approach is to run these stores separate and automatically synchronize data changes. This allows to access the data at a single store and invoke changes across scope boundaries (e.g. triggering an action which changes the values outside will also transfer the value into the iframe
store).
npm install --save redux-sync
1.) Configure the redux store which should be synchronized
import syncStore from 'redux-sync'
// optional
const middlewares = [
thunkMiddleware,
loggerMiddleware
]
const finalCreateStore = compose(
applyMiddleware(...middlewares),
syncStore()
)(createStore)
const store = finalCreateStore(reducer, initialState)
2.) Configure the redux store which should should contain the synchronized store
import syncStore from 'redux-sync'
// optional
const middlewares = [
thunkMiddleware,
loggerMiddleware
]
const finalCreateStore = compose(
applyMiddleware(...middlewares),
syncStore('subtree')
)(createStore)
const store = finalCreateStore(reducer, initialState)
No question it is, but regarding the aspect that this module is meant to ease the state management between different context it provides the advantages of completeness and performance.
Why not using redux persist ?
Indeed using a layer in between which can be accessed from both could be used, but the focus is actual not on persistence (you could still do it if you like) but on communication and optimized synchronization of substates.
- add example
- checkout more lightweight alternatives (current total ~ 39kb), e.g. for patching
- support include/exclude filter options
- integrate webworker and websocket adapter
To build your own version run npm run dev
for development (incl. watch) or npm run build
for production (minified).