API
- 1 ответ
- 0 вопросов
1
Вклад в тег
window.innerWidth
/**
* @example:
* const [width, setWidth] = useState(window.innerWidth);
* useWindowResize(setWidth);
* */
export function useWindowResize(
callback: (width: number) => void,
deps: DependencyList = [],
): void {
const makeThrottledAction = throttle(
() => callback(window.innerWidth),
150,
);
useEffect(() => {
window.addEventListener('resize', makeThrottledAction, { passive: true });
return () => {
window.removeEventListener('resize', makeThrottledAction);
};
}, [...deps]);
}