Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Created October 24, 2021 18:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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