Save as empty value, when trying to save undefined var to local storage (#1443)

* Save empty value, when trying to save undefined var to localstorage

* Lint fix

* Newline at the end of file to satisfy linter?

* Linter again
This commit is contained in:
Danijel Dedic 2021-09-28 17:29:57 +02:00 committed by GitHub
parent d4ebf69bc7
commit c9678c5167
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,8 @@ export default function useLocalStorage<T>(
try {
if (typeof valueToStore === "string") {
localStorage.setItem(key, valueToStore);
} else if (typeof valueToStore === "undefined") {
localStorage.setItem(key, "");
} else {
localStorage.setItem(key, JSON.stringify(valueToStore));
}