Created
January 30, 2021 14:40
-
-
Save danieljpgo/3d8e7cbeca3d82c591da0326c09b3acb to your computer and use it in GitHub Desktop.
useLocalStorageState hook
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
/** | |
* useLocalstorageState hook | |
* @param {String} key The key to set in localStorage for this value | |
* @param {any} initialState The value to use if it is not already in localStorage | |
* @param {{serialize: Function, deserialize: Function}} options The serialize and deserialize | |
* functions to use(defaults to JSON.stringify and JSON.parse respectively) | |
*/ | |
export const useLocalStorageState = <T>( | |
key: string, | |
initialState: T, | |
{ | |
serialize = JSON.stringify, | |
deserialize = JSON.parse, | |
} = {}, | |
): [T, React.Dispatch<React.SetStateAction<T>>] => { | |
const [state, setState] = React.useState(() => { | |
const init = typeof initialState === 'function' ? initialState() : initialState; | |
try { | |
const valueInLocalStorage = window.localStorage.getItem(key); | |
return valueInLocalStorage | |
? deserialize(valueInLocalStorage) | |
: init; | |
} catch (error) { | |
return init; | |
} | |
}); | |
const prevKeyRef = React.useRef(key); | |
React.useEffect(() => { | |
const prevKey = prevKeyRef.current; | |
if (prevKey !== key) { | |
window.localStorage.removeItem(prevKey); | |
} | |
prevKeyRef.current = key; | |
window.localStorage.setItem(key, serialize(state)); | |
}, [key, state, serialize]); | |
return [state, setState]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment