Skip to content

Commit

Permalink
add typeof window check
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Apr 30, 2019
1 parent 128ae40 commit fd71cca
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit fd71cca

Please sign in to comment.