@illinois/react-use-local-storage
Advanced tools
+1
-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; |
+25
-49
| "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); |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,iCAA2C;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA0CE;AAEF,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,YAAY,EAAE,EAAE;IAC5C,IAAI,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE;QAC7C,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAAA;KAC/D;IACD,MAAM,iBAAiB,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAW,CAAC,CAAA;IAChF,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,GAAG,gBAAQ,CAAC,iBAAiB,CAAC,CAAA;IACxD,MAAM,mBAAmB,GAAG,CAAC,CAAC,EAAE;QAC9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OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAAA;gBAC9D,OAAO,YAAY,CAAA;aACpB;iBAAM;gBACL,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAA;aACrC;SACF;QAAC,WAAM;YACN,qDAAqD;YACrD,uEAAuE;YACvE,6BAA6B;YAC7B,OAAO,YAAY,CAAA;SACpB;IACH,CAAC,CAAC,CAAA;IACF,MAAM,mBAAmB,GAAG,CAAC,CAAe,EAAE,EAAE;QAC9C,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,EAAE;YACjB,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,QAAkB,CAAC,CAAC,CAAA;SAC9C;IACH,CAAC,CAAA;IACD,MAAM,QAAQ,GAAG,CAAC,KAAQ,EAAE,EAAE;QAC5B,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;QACvD,WAAW,CAAC,KAAK,CAAC,CAAA;IACpB,CAAC,CAAA;IACD,iBAAS,CAAC,GAAG,EAAE;QACb,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAA;QACvD,OAAO,GAAG,EAAE;YACV,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAA;QAC5D,CAAC,CAAA;IACH,CAAC,CAAC,CAAA;IACF,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;AAC1B,CAAC,CAAA;AAED,kBAAe,eAAe,CAAA"} |
+1
-1
| { | ||
| "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", |
6581
-5.97%48
-32.39%