Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Created October 24, 2021 18:12
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 jongacnik/b1a018ade118e12a04fe3e6c651447b2 to your computer and use it in GitHub Desktop.
Save jongacnik/b1a018ade118e12a04fe3e6c651447b2 to your computer and use it in GitHub Desktop.
Querystate w/ history
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