Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
redux-state-sync
Advanced tools
A light weight middleware to sync your redux state across browser tabs. This module will listens to the window storage event and triggers exactly the same actions triggered in other tabs to make the redux state sync. Furthermore you can also passing in an ignore list to ignore the actions which you don't want to trigger in other tabs(like Api requests).
Simply install it with npm.
npm install --save redux-state-sync
import actionStorageMiddleware and createStorageListener to your project and follow the example below.
import { actionStorageMiddleware, createStorageListener } from 'redux-state-sync';
/*
* actionStorageMiddleware is used to persist your last action being triggered.
*/
const middlewares = [
sagaMiddleware,
logger,
actionStorageMiddleware,
routerMiddleware(history),
];
const store = createStore(
createReducer(),
fromJS(initialState),
applyMiddleware(...middlewares)
);
/*
* Create a listener on store to dispatch the latest action being triggered on other tabs.
*/
createStorageListener(store);
You may not want to trigger the Actions which triggers a api request. So you can simply pass in the Action type to ignore it. Like below:
const config = {
ignore: ['CHANGE_USERNAME', 'REPO_REQUEST'],
};
createStorageListener(store, config);
/*
* To still make your state sync. You need to trigger other actions with the data from the api request.
* The example is using redux-saga to handle the side effects.
*/
export function* fetchRepoSaga(action) {
try {
const repo = yield call(api.fetchRepo, action.url);
/* Triggers a REPO_SUCCESS and this action should be triggered in other tabs also */
yield put({ type: 'REPO_SUCCESS', repo });
} catch (e) {
yield put({ type: 'REPO_FAILURE', message: e.message });
}
}
export function* fetchRepoWatcher() {
yield* takeLatest(REPO.REQUEST, fetchRepoSaga);
}
export default [
fetchRepoWatcher,
];
FAQs
A middleware for redux to sync state in different tabs
The npm package redux-state-sync receives a total of 23,003 weekly downloads. As such, redux-state-sync popularity was classified as popular.
We found that redux-state-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.