Consistent reuse of isClient util

This commit is contained in:
Ward Oosterlijnck 2019-03-31 16:42:29 +11:00
parent 908f73be07
commit 94b4ea9d3e
5 changed files with 6 additions and 8 deletions

View File

@ -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(() => {

View File

@ -1,6 +1,5 @@
import {useState, useEffect} from 'react';
const isClient = typeof window === 'object';
import {isClient} from './util';
const useLocalStorage = <T>(key: string, initialValue?: T, raw?: boolean): [T, (value: T) => void] => {
if (!isClient) {

View File

@ -1,6 +1,5 @@
import {useState, useEffect} from 'react';
const isClient = typeof window === 'object';
import {isClient} from './util';
const useSessionStorage = <T>(key: string, initialValue?: T, raw?: boolean): [T, (value: T) => void] => {
if (!isClient) {

View File

@ -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<any>) | React.ReactElement<any>;

View File

@ -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}>({