Refactor useWindowWidth hook to improve readability
This commit is contained in:
parent
40f1384dfc
commit
8da220e990
1 changed files with 4 additions and 9 deletions
|
@ -1,19 +1,14 @@
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
const useWindowWidth = () => {
|
function useWindowWidth() {
|
||||||
const [windowWidth, setWindowWidth] = useState()
|
const [width, setWidth] = useState(window.innerWidth)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleResize = () => {
|
const handleResize = () => setWidth(window.innerWidth)
|
||||||
setWindowWidth(window.innerWidth)
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('resize', handleResize)
|
window.addEventListener('resize', handleResize)
|
||||||
|
|
||||||
// Cleanup function to remove the event listener
|
|
||||||
return () => window.removeEventListener('resize', handleResize)
|
return () => window.removeEventListener('resize', handleResize)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return windowWidth
|
return width
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useWindowWidth
|
export default useWindowWidth
|
||||||
|
|
Loading…
Add table
Reference in a new issue