ant-design/components/grid/hooks/useBreakpoint.tsx
azro352 328a800132
test: add test cases for breakpoints customize (#39697)
Rename useResponsiveObserve to useResponsiveObserver. Add validation of breakpoints values and test
2022-12-22 10:11:08 +08:00

26 lines
778 B
TypeScript

import { useEffect, useRef } from 'react';
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();
useEffect(() => {
const token = responsiveObserver.subscribe((supportScreens) => {
screensRef.current = supportScreens;
if (refreshOnChange) {
forceUpdate();
}
});
return () => responsiveObserver.unsubscribe(token);
}, []);
return screensRef.current;
}
export default useBreakpoint;