This repository has been archived on 2025-09-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Netflix-Clone/src/hooks/useViewport.js
T

40 lines
963 B
JavaScript

import { useState, useEffect } from 'react'
const height =
window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight
const width =
window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth
export const useViewport = () => {
const [windowDimensions, setWindowDimensions] = useState({ height, width })
const deriveWindowDimensions = () => {
const height =
window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight
const width =
window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth
setWindowDimensions({ height, width })
}
useEffect(() => {
deriveWindowDimensions()
window.addEventListener('resize', deriveWindowDimensions)
return () => {
window.removeEventListener('resize', deriveWindowDimensions)
}
}, [])
return [windowDimensions]
}