Skip to content

Commit

Permalink
更新 hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sumingcheng committed Dec 15, 2024
1 parent 11e504e commit b0bdd12
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/utils/deviceUtils.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import React from 'react'
import { throttle } from 'lodash'

export const isMobile = () => {
if (typeof window === 'undefined') return false
return window.innerWidth <= 768
}

export const useIsMobile = () => {
const [isMobileView, setIsMobileView] = React.useState(isMobile())
const [isMobileView, setIsMobileView] = React.useState(false)

React.useEffect(() => {
const handleResize = () => {
setIsMobileView(isMobile())

const handleResize = throttle(() => {
setIsMobileView(isMobile())
}
}, 200)

window.addEventListener('resize', handleResize)
return () => window.removeEventListener('resize', handleResize)
window.addEventListener('orientationchange', handleResize)

return () => {
window.removeEventListener('resize', handleResize)
window.removeEventListener('orientationchange', handleResize)
}
}, [])

return isMobileView
Expand Down

0 comments on commit b0bdd12

Please sign in to comment.