Last active
March 25, 2021 21:28
-
-
Save diegocardoso93/d2aeec978f89aef3dfe203e7dddd235c to your computer and use it in GitHub Desktop.
simple localstorage hook abstraction
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
export default function usePersist(target) { | |
const setItem = (key, value) => | |
localStorage.setItem(key, JSON.stringify(value)); | |
const set = target | |
? (value) => setItem(target, value) | |
: (key, value) => setItem(key, value); | |
const getItem = (key) => JSON.parse(localStorage.getItem(key)); | |
const get = target ? getItem(target) : (key) => getItem(key); | |
return [get, set]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to use
option 1:
option 2: