Created
October 24, 2021 18:12
-
-
Save jongacnik/b1a018ade118e12a04fe3e6c651447b2 to your computer and use it in GitHub Desktop.
Querystate w/ history
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 querystate from 'querystate' | |
import nanobus from 'nanobus' | |
const state = querystate() | |
const bus = nanobus() | |
// emit state on history change | |
;(function(history){ | |
var pushState = history.pushState | |
var popState = history.popState | |
history.pushState = function(s) { | |
bus.emit('history', state) | |
return pushState.apply(history, arguments) | |
} | |
window.onpopstate = function (s) { | |
const urlParams = new URLSearchParams(window.location.search) | |
urlParams.forEach((value, key) => { | |
state.queryState[key] = value | |
}) | |
bus.emit('history', state) | |
} | |
})(window.history); | |
export { state as default, state, bus } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment