Skip to content

Instantly share code, notes, and snippets.

@maximgatilin
Created July 4, 2019 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximgatilin/8244dadff0fed3a92a9028ce2a7eb85f to your computer and use it in GitHub Desktop.
Save maximgatilin/8244dadff0fed3a92a9028ce2a7eb85f to your computer and use it in GitHub Desktop.
react router v4 middleware
import { push, replace } from 'connected-react-router';
export default store => next => action => {
if (action.pushToUrl) {
const state = store.getState();
const currentQueries = new URLSearchParams(state.router.location.search);
Object.entries(action.pushToUrl).forEach(([key, value]) => {
currentQueries.set(key, value);
});
const queriesAsString = currentQueries.toString();
store.dispatch(push(
`${state.router.location.pathname}?${queriesAsString}`,
));
}
if (action.updateUrl) {
store.dispatch(push(action.updateUrl));
}
if (action.replaceUrl) {
store.dispatch(replace(action.replaceUrl));
}
return next(action);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment