@illinois/react-use-local-storage
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
declare const useLocalStorage: (key: any, initialValue: any) => any[]; | ||
declare const useLocalStorage: <T>(key: string, initialValue: T) => [T, (value: T) => void]; | ||
export default useLocalStorage; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_1 = require("react"); | ||
/* | ||
const isClient = typeof window !== 'undefined' | ||
const useLocalStorage = <T>(key: string, initialValue: T): [T, (value: T) => void] => { | ||
if (!isClient) { | ||
// We're SSRing; can't use local storage here! | ||
return [initialValue, () => {}] | ||
} | ||
const [state, updateState] = useState((): T => { | ||
try { | ||
const localStorageValue = window.localStorage.getItem(key) | ||
if (localStorageValue === null) { | ||
// Initialize local storage with default state | ||
window.localStorage.setItem(key, JSON.stringify(initialValue)) | ||
return initialValue | ||
} else { | ||
return JSON.parse(localStorageValue) | ||
} | ||
} catch { | ||
// User might be facing storage restrictions, or JSON | ||
// serialization/deserialization may have failed. We can just fall back | ||
// to using React state here. | ||
return initialValue | ||
} | ||
}) | ||
const localStorageChanged = (e: StorageEvent) => { | ||
if (e.key === key) { | ||
updateState(JSON.parse(e.newValue as string)) | ||
} | ||
} | ||
const setState = (value: T) => { | ||
window.localStorage.setItem(key, JSON.stringify(value)) | ||
updateState(value) | ||
} | ||
useEffect(() => { | ||
window.addEventListener('storage', localStorageChanged) | ||
return () => { | ||
window.removeEventListener('storage', localStorageChanged) | ||
} | ||
}) | ||
return [state, setState] | ||
} | ||
*/ | ||
const isClient = typeof window !== 'undefined'; | ||
const useLocalStorage = (key, initialValue) => { | ||
if (window.localStorage.getItem(key) === null) { | ||
window.localStorage.setItem(key, JSON.stringify(initialValue)); | ||
if (!isClient) { | ||
// We're SSRing; can't use local storage here! | ||
return [initialValue, () => { }]; | ||
} | ||
const localStorageValue = JSON.parse(window.localStorage.getItem(key)); | ||
const [state, updateState] = react_1.useState(localStorageValue); | ||
const localStorageChanged = e => { | ||
const [state, updateState] = react_1.useState(() => { | ||
try { | ||
const localStorageValue = window.localStorage.getItem(key); | ||
if (localStorageValue === null) { | ||
// Initialize local storage with default state | ||
window.localStorage.setItem(key, JSON.stringify(initialValue)); | ||
return initialValue; | ||
} | ||
else { | ||
return JSON.parse(localStorageValue); | ||
} | ||
} | ||
catch (_a) { | ||
// User might be facing storage restrictions, or JSON | ||
// serialization/deserialization may have failed. We can just fall back | ||
// to using React state here. | ||
return initialValue; | ||
} | ||
}); | ||
const localStorageChanged = (e) => { | ||
if (e.key === key) { | ||
@@ -58,3 +34,3 @@ updateState(JSON.parse(e.newValue)); | ||
}; | ||
const setState = value => { | ||
const setState = (value) => { | ||
window.localStorage.setItem(key, JSON.stringify(value)); | ||
@@ -61,0 +37,0 @@ updateState(value); |
{ | ||
"name": "@illinois/react-use-local-storage", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "React hook that persists and syncs state with local storage", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6581
48