Skip to main content

useWindowResize

Source ↗

Runs a callback whenever the browser window is resized. The callback ref is kept stable so the event listener is only attached once. Used internally by the Popover component for repositioning.

Import​

import { useWindowResize } from '@andrejground/lab';

Usage​

import React from 'react';
import { useWindowResize } from '@andrejground/lab';

export default function App() {
const [width, setWidth] = React.useState(window.innerWidth);

useWindowResize(() => {
setWidth(window.innerWidth);
});

return <p>Window width: {width}px</p>;
}

API​

useWindowResize(callback);
ParameterTypeDescription
callback(e?: UIEvent) => voidCalled on every resize event