From fd71ccaccb8d4a99c5e7be91d91ab050cbc06ecf Mon Sep 17 00:00:00 2001 From: sw-yx Date: Tue, 30 Apr 2019 01:27:01 -0400 Subject: [PATCH] add typeof window check --- src/index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 9ab179b..0e23f12 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -67,7 +67,7 @@ export function useInput( let _initialValue = initialValue // safely check localstorage and coerce the right types - if (window && window.localStorage && typeof localStorageName === "string") { + if (typeof window !== 'undefined' && window.localStorage && typeof localStorageName === "string") { let v = localStorage.getItem(localStorageName) if (v && typeof initialValue === "number") _initialValue = Number(v) else if (v && Array.isArray(v)) _initialValue = v.split(STRINGARRAYSERIALIZER) @@ -83,7 +83,7 @@ export function useInput( } const val = e.target.value setValue(val) - if (window && window.localStorage && typeof localStorageName === "string") { + if (typeof window !== 'undefined' && window.localStorage && typeof localStorageName === "string") { if (val !== initialValue) { localStorage.setItem(localStorageName, String(Array.isArray(val) ? val.join(STRINGARRAYSERIALIZER) : val)) } else { @@ -128,7 +128,7 @@ export function useCheckInput( let _initialValue = initialValue // safely check localstorage and coerce the right types - if (window && window.localStorage && typeof localStorageName === "string") { + if (typeof window !== 'undefined' && window.localStorage && typeof localStorageName === "string") { let v = localStorage.getItem(localStorageName) _initialValue = v === "true" // dont cast strings with Boolean lol } @@ -140,7 +140,7 @@ export function useCheckInput( } const val = e.target.checked setValue(val) - if (window && window.localStorage && typeof localStorageName === "string") { + if (typeof window !== 'undefined' && window.localStorage && typeof localStorageName === "string") { if (val !== initialValue) { localStorage.setItem(localStorageName, String(val)) } else {