hunt-blacklist-frontend/app/hooks/windowDimensions.ts

27 lines
1021 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState, useEffect } from "react";
export default function useWindowDimensions() {
// На сервере инициализируем значениями по умолчанию (0 или null)
const [dimensions, setDimensions] = useState({
width: typeof window !== 'undefined' ? window.innerWidth : 0,
height: typeof window !== 'undefined' ? window.innerHeight : 0,
});
useEffect(() => {
// Этот код гарантированно выполнится ТОЛЬКО в браузере
function handleResize() {
setDimensions({
width: window.innerWidth,
height: window.innerHeight,
});
}
// Устанавливаем точные размеры сразу при монтировании
handleResize();
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
return dimensions;
}