mirror of https://github.com/fantasticit/think.git
client: detect window resize
This commit is contained in:
parent
c7d70f6ee9
commit
2b64a7c09f
|
@ -1,8 +1,25 @@
|
|||
import { useEffect } from 'react';
|
||||
|
||||
import { createGlobalHook } from './create-global-hook';
|
||||
import { useToggle } from './use-toggle';
|
||||
|
||||
const PC_MOBILE_CRITICAL_WIDTH = 765;
|
||||
|
||||
const useOnMobile = (defaultIsMobile) => {
|
||||
const [isMobile, toggleIsMobile] = useToggle(defaultIsMobile);
|
||||
|
||||
useEffect(() => {
|
||||
function handleResize() {
|
||||
toggleIsMobile(window.innerWidth <= PC_MOBILE_CRITICAL_WIDTH);
|
||||
}
|
||||
|
||||
window.addEventListener('resize', handleResize);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('resize', handleResize);
|
||||
};
|
||||
}, [toggleIsMobile]);
|
||||
|
||||
return {
|
||||
isMobile,
|
||||
toggleIsMobile,
|
||||
|
|
Loading…
Reference in New Issue