mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-05 05:28:20 +08:00
502dac12aa
* docs: fix code * feat: lint * feat: prettier * feat: test * feat: review * feat: format html * feat: format html
28 lines
839 B
TypeScript
28 lines
839 B
TypeScript
import { useRef } from 'react';
|
|
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
|
|
|
|
import useForceUpdate from '../../_util/hooks/useForceUpdate';
|
|
import type { ScreenMap } from '../../_util/responsiveObserver';
|
|
import useResponsiveObserver from '../../_util/responsiveObserver';
|
|
|
|
function useBreakpoint(refreshOnChange: boolean = true): ScreenMap {
|
|
const screensRef = useRef<ScreenMap>({});
|
|
const forceUpdate = useForceUpdate();
|
|
const responsiveObserver = useResponsiveObserver();
|
|
|
|
useLayoutEffect(() => {
|
|
const token = responsiveObserver.subscribe((supportScreens) => {
|
|
screensRef.current = supportScreens;
|
|
if (refreshOnChange) {
|
|
forceUpdate();
|
|
}
|
|
});
|
|
|
|
return () => responsiveObserver.unsubscribe(token);
|
|
}, []);
|
|
|
|
return screensRef.current;
|
|
}
|
|
|
|
export default useBreakpoint;
|