Created
May 29, 2017 19:30
-
-
Save nexus-uw/96bce6689753e15add08f9d2e508094e to your computer and use it in GitHub Desktop.
save/retrieve state in the query string
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
function saveParamsToUrlQuery(state) { | |
const queryString = Object.keys(state) | |
.reduce((res, key) => state[key] ? | |
res += `${encodeURIComponent(key)}=${encodeURIComponent(state[key])}&` : | |
res, | |
''); | |
history.pushState({}, 'generate-routes', window.location.pathname + '?' + queryString); | |
} | |
function loadInputFromUrl() { | |
const queryStrings = (window.location.search.split('?')[1] || '').split('&'); | |
return queryStrings.reduce((result, qs) => { | |
const f = qs.split('='); | |
result[decodeURIComponent(f[0])] = decodeURIComponent(f[1]); | |
return result; | |
}, {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment