Skip to content

Commit

Permalink
fix: πŸ› don't throw only in development
Browse files Browse the repository at this point in the history
Throwing changes behaviour of the code, other code may rely on this
behaviour, so we either throw in all environments or console.error
instead.

BREAKING CHANGE: 🧨 error is logged instead of thrown in development environment
  • Loading branch information
streamich committed Mar 26, 2019
1 parent 2b3dd23 commit f5faba5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/useGetSetState.ts
Expand Up @@ -4,7 +4,7 @@ import useUpdate from './useUpdate';
const useGetSetState = <T extends object>(initialState: T = {} as T): [() => T, (patch: Partial<T>) => void]=> {
if (process.env.NODE_ENV !== 'production') {
if (typeof initialState !== 'object') {
throw new TypeError('useGetSetState initial state must be an object.');
console.error('useGetSetState initial state must be an object.');
}
}

Expand All @@ -15,7 +15,7 @@ const useGetSetState = <T extends object>(initialState: T = {} as T): [() => T,
if (!patch) return;
if (process.env.NODE_ENV !== 'production') {
if (typeof patch !== 'object') {
throw new TypeError('useGetSetState setter patch must be an object.');
console.error('useGetSetState setter patch must be an object.');
}
}
Object.assign(state.current, patch);
Expand Down
2 changes: 1 addition & 1 deletion src/useHoverDirty.ts
Expand Up @@ -4,7 +4,7 @@ import {useEffect, useState} from 'react';
const useHoverDirty = (ref, enabled: boolean = true) => {
if (process.env.NODE_ENV === 'development') {
if ((typeof ref !== 'object') || (typeof ref.current === 'undefined')) {
throw new TypeError('useHoverDirty expects a single ref argument.');
console.error('useHoverDirty expects a single ref argument.');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/useMouse.ts
Expand Up @@ -15,7 +15,7 @@ export interface State {
const useMouse = (ref: RefObject<HTMLElement>, whenHovered: boolean = false): State => {
if (process.env.NODE_ENV === 'development') {
if ((typeof ref !== 'object') || (typeof ref.current === 'undefined')) {
throw new TypeError('useMouse expects a single ref argument.');
console.error('useMouse expects a single ref argument.');
}
}

Expand Down

4 comments on commit f5faba5

@deevus
Copy link

@deevus deevus commented on f5faba5 Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does useHoverDirty error at all? When using useRef, ref.current is always undefined on the first render, at least in Chrome.

@deevus
Copy link

@deevus deevus commented on f5faba5 Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this applies to the other hooks as well

@streamich
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, it expects .current to be null.

@wardoost
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it expects .current to be null. Similar parameter check has been used here:

const useMouse = (ref: RefObject<HTMLElement>): State => {
if (process.env.NODE_ENV === 'development') {
if ((typeof ref !== 'object') || (typeof ref.current === 'undefined')) {
console.error('useMouse expects a single ref argument.');
}
}

There are more hooks in this repo that take a ref parameter but they don't always do a parameter check. For example useFullscreen doesn't do anything or show a warning when no is passed in:
const useFullscreen = (ref: RefObject<Element>, on: boolean, options: FullScreenOptions = {}): boolean => {
const {video, onClose = noop} = options;
const [isFullscreen, setIsFullscreen] = useState(on);
useLayoutEffect(() => {
if (!on) return;
if (!ref.current) return;
We should try to align error and warning messages between hooks more.

Please sign in to comment.