Files
30-seconds-of-code/snippets/useOnWindowResize.md
Isabelle Viktoria Maciohsek e7988c0065 Update formatting
2022-01-30 19:41:09 +02:00

1.1 KiB

title, tags, firstSeen
title tags firstSeen
useOnWindowResize hooks,effect,event,intermediate 2021-12-01T05:00:00-04:00

Executes a callback whenever the window is resized.

  • Use the useRef() hook to create a variable, listener, which will hold the listener reference.
  • Use the useEffect() hook and EventTarget.addEventListener() to listen to the 'resize' event of the Window global object.
  • Use EventTarget.removeEventListener() to remove any existing listeners and clean up when the component unmounts.
const useOnWindowResize = callback => {
  const listener = React.useRef(null);

  React.useEffect(() => {
    if (listener.current)
      window.removeEventListener('resize', listener.current);
    listener.current = window.addEventListener('resize', callback);
    return () => {
      window.removeEventListener('resize', listener.current);
    };
  }, [callback]);
};
const App = () => {
  useOnWindowResize(() =>
    console.log(`window size: (${window.innerWidth}, ${window.innerHeight})`)
  );
  return <p>Resize the window and check the console</p>;
};

ReactDOM.render(<App />, document.getElementById('root'));