Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { useState, useEffect } from 'react';
// Usage
function App() {
const size = useWindowSize();
return (
<div>
{size.width}px / {size.height}px
</div>
);
}
// Hook
function useWindowSize() {
const isClient = typeof window === 'object';
function getSize() {
return {
width: isClient ? window.innerWidth : undefined,
height: isClient ? window.innerHeight : undefined
};
}
const [windowSize, setWindowSize] = useState(getSize);
useEffect(() => {
if (!isClient) {
return false;
}
function handleResize() {
setWindowSize(getSize());
}
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []); // Empty array ensures that effect is only run on mount and unmount
return windowSize;
}
@mattfysh

This comment has been minimized.

Copy link

commented Oct 31, 2018

You could pass a function into useState, so the initial state is calculated only once (and not on each render), i.e:

const [windowSize, setWindowSize] = useState(getSize)
@gragland

This comment has been minimized.

Copy link
Owner Author

commented Oct 31, 2018

@mattfysh Good catch! Will update shortly.

@AndyBarron

This comment has been minimized.

Copy link

commented Oct 31, 2018

Couldn't we declare isClient and getSize in the module scope so that they're only checked/allocated once? I would also be a fan of returning [width, height] as a tuple, but that's super subjective.

Also, the useEffect callback unconditionally accesses window, so the isClient check wouldn't prevent you from crashing in a non-client environment.

import { useState, useEffect } from 'react';

// Usage
function App() {
  const [width, height] = useWindowSize();

  return (
    <div>
      {width}px / {height}px
    </div>
  );
}

// Helpers
const IS_CLIENT = typeof window === 'object';
const getSize = IS_CLIENT ?
  () => [window.innerWidth, window.innerHeight] :
  () => [undefined, undefined];

// Hook
function useWindowSize() {
  const [windowSize, setWindowSize] = useState(getSize);

  useEffect(() => {
    if (!IS_CLIENT) {
      return;
    }
    
    function handleResize() {
      setWindowSize(getSize());
    };
    
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []); // Empty array ensures that effect is only run on mount and unmount

  return windowSize;
}
@gragland

This comment has been minimized.

Copy link
Owner Author

commented Nov 2, 2018

@AndyBarron Thanks, fixed the issue with isClient not being checked within useEffect. And agree that isClient and getSize should probably be in module scope. For the time being, I'm trying to keep the usage and hook functions fairly self contained so it's easier to copy one or the other into a project without missing anything.. but I'll likely resolve that soon by breaking the two up into separate code blocks, in which case I'll then move stuff out into module scope where it makes sense. Really appreciate the detailed feedback you've been giving on all the hooks!

@mattfysh

This comment has been minimized.

Copy link

commented Nov 5, 2018

Also, the useEffect callback unconditionally accesses window, so the isClient check wouldn't prevent you from crashing in a non-client environment.

Given that useEffect somewhat relates to componentDidMount and componentDidUpdate, does this mean they are not executed in server-side rendering contexts? My hunch is that they aren't invoked, and the access of window should be okay?

@ianobermiller

This comment has been minimized.

Copy link

commented Nov 9, 2018

I suggest moving handleResize into useEffect as in @AndyBarron's example

@gustavoguichard

This comment has been minimized.

Copy link

commented Nov 12, 2018

An optional debounce value would fit nicely here ;)

@gragland

This comment has been minimized.

Copy link
Owner Author

commented Nov 20, 2018

@yazeedb

This comment has been minimized.

Copy link

commented Nov 25, 2018

@gragland Thanks for this hook, I'm using it on a project!
May I write a brief blog post on it? Will link back to you and useHooks.com

@olee

This comment has been minimized.

Copy link

commented Dec 30, 2018

This version uses an array as result (because it's just better for destructuring) and has more options with smaller footprint.

import { useState, useEffect } from 'react';

type Result = [number | undefined, number | undefined];

/**
 * Returns current window size and updates state if window if resized
 * @param  element            Optional element to use for accessing the window object instead of global window variable
 * @param  disableResizeEvent Disable the resize event and only get initial window size
 * @return [width, height]
 */
export default function useWindowSize(element?: HTMLElement | null, disableResizeEvent?: boolean): Result {
    // If we are not even in a browser environment, do not even useEffect - window size will always be undefined
    if (typeof window !== 'object')
        return [undefined, undefined];
    // Get reference to window objecz
    const wnd = element && element.ownerDocument && element.ownerDocument.defaultView || window;
    // Get / initialize state
    const [result, setSize] = useState<Result>([wnd && wnd.innerWidth, wnd && wnd.innerHeight]);
    useEffect(() => {
        if (typeof wnd === 'object') {
            const handleResize = () => setSize([wnd.innerWidth, wnd.innerHeight]);
            handleResize();
            if (!disableResizeEvent) {
                wnd.addEventListener('resize', handleResize);
                return () => wnd.removeEventListener('resize', handleResize);
            }
        }
    }, [wnd, disableResizeEvent]);
    return result;
}
@antifriz

This comment has been minimized.

Copy link

commented Sep 21, 2019

When using typescript and @types/react:16.9.2 effect callback is defined as follows:

type EffectCallback = () => (void | (() => void | undefined))

this leads to returning false being treated as invalid return type.
I'd suggest to replace it just with return; or return () => {} not to be bothered with inconsistent return points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.