Skip to content

Commit 1565747

Browse files
use screen width hook
1 parent f884e1d commit 1565747

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/hooks/useScreenWidth.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useEffect, useState } from 'react'
2+
3+
export const useScreenWidth = (): 'MOBILE' | 'TABLET' | 'DESKTOP' => {
4+
const [screenWidth, setScreenWidth] = useState<
5+
'MOBILE' | 'TABLET' | 'DESKTOP'
6+
>('MOBILE')
7+
8+
useEffect(() => {
9+
const handleScreenWidth = () => {
10+
if (window.innerWidth < 640) {
11+
setScreenWidth('MOBILE')
12+
} else if (window.innerWidth < 1024) {
13+
setScreenWidth('TABLET')
14+
} else setScreenWidth('DESKTOP')
15+
}
16+
window.addEventListener('resize', handleScreenWidth)
17+
handleScreenWidth()
18+
19+
return () => {
20+
window.removeEventListener('resize', handleScreenWidth)
21+
}
22+
}, [])
23+
24+
return screenWidth
25+
}

0 commit comments

Comments
 (0)