Created
July 4, 2019 10:55
-
-
Save maximgatilin/8244dadff0fed3a92a9028ce2a7eb85f to your computer and use it in GitHub Desktop.
react router v4 middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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