Skip to content

Commit

Permalink
Consistent reuse of isClient util
Browse files Browse the repository at this point in the history
  • Loading branch information
wardoost committed Mar 31, 2019
1 parent 908f73b commit 94b4ea9
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/useEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {useEffect} from 'react';
import {isClient} from './util';

export interface ListenerType1 {
addEventListener (name: string, handler: (event?: any) => void, ...args: any[]);
Expand All @@ -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(() => {
Expand Down
3 changes: 1 addition & 2 deletions src/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/useSessionStorage.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/useSize.tsx
Original file line number Diff line number Diff line change
@@ -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>;
Expand Down
3 changes: 1 addition & 2 deletions src/useWindowSize.ts
Original file line number Diff line number Diff line change
@@ -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}>({
Expand Down

0 comments on commit 94b4ea9

Please sign in to comment.