Skip to content

Commit

Permalink
Hooks Update
Browse files Browse the repository at this point in the history
- add window check to useMatchMedia
- update window check conditional for useWindowSize
  • Loading branch information
heyjul3s committed Jan 26, 2021
1 parent 097b953 commit 222fc3c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/usematchmedia/src/useMatchMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ export function useMatchMedia(query: string): boolean {
}, []);

useEffect(() => {
matchListRef.current = window.matchMedia(query);
matchListRef.current.addEventListener('change', onMediaQueryListEvent);
setIsMatch(matchListRef.current.matches);
if (!!window) {
matchListRef.current = window.matchMedia(query);
matchListRef.current.addEventListener('change', onMediaQueryListEvent);
setIsMatch(matchListRef.current.matches);
} else {
console.error('Error: typeof "window" is undefined.');
setIsMatch(false);
}

return function unmountUseMatchMedia() {
if (matchListRef.current) {
Expand Down
2 changes: 1 addition & 1 deletion packages/usewindowsize/src/useWindowSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function useWindowSize(): WindowSize {
const [windowSize, setWindowSize] = useState<WindowSize>(getSize());

function getSize() {
if (typeof window !== 'undefined') {
if (!!window) {
return {
innerHeight: window.innerHeight,
innerWidth: window.innerWidth,
Expand Down

0 comments on commit 222fc3c

Please sign in to comment.