diff --git a/src/useEvent.ts b/src/useEvent.ts index 3bd51b4e..0f30b949 100644 --- a/src/useEvent.ts +++ b/src/useEvent.ts @@ -1,4 +1,5 @@ import {useEffect} from 'react'; +import {isClient} from './util'; export interface ListenerType1 { addEventListener (name: string, handler: (event?: any) => void, ...args: any[]); @@ -12,7 +13,7 @@ export interface ListenerType2 { export type UseEventTarget = ListenerType1 | ListenerType2; -const defaultTarget = typeof window === 'object' ? window : null; +const defaultTarget = isClient ? window : null; const useEvent = (name: string, handler?: null | undefined | ((event?: any) => void), target: null | UseEventTarget = defaultTarget, options?: any) => { useEffect(() => { diff --git a/src/useLocalStorage.ts b/src/useLocalStorage.ts index 78a9671c..24d95a22 100644 --- a/src/useLocalStorage.ts +++ b/src/useLocalStorage.ts @@ -1,6 +1,5 @@ import {useState, useEffect} from 'react'; - -const isClient = typeof window === 'object'; +import {isClient} from './util'; const useLocalStorage = (key: string, initialValue?: T, raw?: boolean): [T, (value: T) => void] => { if (!isClient) { diff --git a/src/useSessionStorage.ts b/src/useSessionStorage.ts index 8621bd99..2c09310a 100644 --- a/src/useSessionStorage.ts +++ b/src/useSessionStorage.ts @@ -1,6 +1,5 @@ import {useState, useEffect} from 'react'; - -const isClient = typeof window === 'object'; +import {isClient} from './util'; const useSessionStorage = (key: string, initialValue?: T, raw?: boolean): [T, (value: T) => void] => { if (!isClient) { diff --git a/src/useSize.tsx b/src/useSize.tsx index 49c9882b..3c2baa65 100644 --- a/src/useSize.tsx +++ b/src/useSize.tsx @@ -1,8 +1,8 @@ import * as React from 'react'; +import {isClient} from './util'; const {useState, useEffect, useRef} = React; -const isClient = typeof window === 'object'; const DRAF = (callback: () => void) => setTimeout(callback, 35); export type Element = ((state: State) => React.ReactElement) | React.ReactElement; diff --git a/src/useWindowSize.ts b/src/useWindowSize.ts index 2e115a92..7ed04c75 100644 --- a/src/useWindowSize.ts +++ b/src/useWindowSize.ts @@ -1,6 +1,5 @@ import {useState, useEffect} from 'react'; - -const isClient = typeof window === 'object'; +import {isClient} from './util'; const useWindowSize = (initialWidth = Infinity, initialHeight = Infinity) => { const [state, setState] = useState<{width: number, height: number}>({