mirror of
https://github.com/fantasticit/think.git
synced 2026-11-02 16:23:15 +08:00
client: detect window resize
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user