We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f884e1d commit 1565747Copy full SHA for 1565747
src/hooks/useScreenWidth.ts
@@ -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