use-local-storage-state
Advanced tools
Comparing version 5.1.0 to 6.0.0
@@ -18,3 +18,3 @@ "use strict"; | ||
catch (_b) { | ||
return getUnderlyingValue(defaultValue); | ||
return unwrapValue(defaultValue); | ||
} | ||
@@ -42,3 +42,3 @@ }, | ||
const initializedStorageKeys = new Set(); | ||
const getUnderlyingValue = (valueOrCallback) => { | ||
const unwrapValue = (valueOrCallback) => { | ||
const isCallable = (value) => typeof value === 'function'; | ||
@@ -48,5 +48,6 @@ return isCallable(valueOrCallback) ? valueOrCallback() : valueOrCallback; | ||
function useLocalStorageState(key, defaultValue) { | ||
const defaultValueRef = react_1.useRef(defaultValue); | ||
const [state, setState] = react_1.useState(() => { | ||
return { | ||
value: storage.get(key, defaultValue), | ||
value: storage.get(key, defaultValueRef.current), | ||
isPersistent: (() => { | ||
@@ -81,3 +82,3 @@ if (typeof window === 'undefined') { | ||
setState({ | ||
value: getUnderlyingValue(defaultValue), | ||
value: unwrapValue(defaultValueRef.current), | ||
isPersistent: state.isPersistent, | ||
@@ -87,3 +88,3 @@ }); | ||
return fn; | ||
}, [key, defaultValue]); | ||
}, [key]); | ||
react_1.useEffect(() => { | ||
@@ -105,3 +106,3 @@ if (initializedStorageKeys.has(key)) { | ||
setState({ | ||
value: storage.get(key, defaultValue), | ||
value: storage.get(key, defaultValueRef.current), | ||
isPersistent: false, | ||
@@ -113,3 +114,3 @@ }); | ||
return () => window.removeEventListener('storage', onStorage); | ||
}, [defaultValue]); | ||
}, []); | ||
return [state.value, updateValue, state.isPersistent]; | ||
@@ -116,0 +117,0 @@ } |
{ | ||
"name": "use-local-storage-state", | ||
"version": "5.1.0", | ||
"version": "6.0.0", | ||
"description": "React hook that persist data in local storage. Done right.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
13130
148