Skip to content

Commit

Permalink
fix: πŸ› make useWindowSize work on server
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 4, 2019
1 parent d850e97 commit 8f93853
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/useWindowSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ const useWindowSize = (initialWidth = Infinity, initialHeight = Infinity) => {
});

useEffect(() => {
const handler = () => {
setState({
width: window.innerWidth,
height: window.innerHeight
});
};
window.addEventListener('resize', handler);
return () => window.removeEventListener('resize', handler);
}, [1]);
if (isClient) {
const handler = () => {
setState({
width: window.innerWidth,
height: window.innerHeight
});
};
window.addEventListener('resize', handler);
return () => window.removeEventListener('resize', handler);
} else {
return undefined;
}
}, []);

return state;
};
Expand Down

0 comments on commit 8f93853

Please sign in to comment.