message: optimize: useResponsive
diff: @@ -21,20 +21,17 @@ let responsiveConfig: ResponsiveConfig = {
  xl: 1200,
  };
  -function init() {
  - if (info || typeof window === "undefined") return;
  - info = {};
  - calculate();
  - window.addEventListener('resize', () => {
  +function handleResize() {
  const oldInfo = info;
  calculate();
  if (oldInfo === info) return;
  for (const subscriber of subscribers) {
  subscriber();
  }
  - });
  }
  +let listening = false;
  +
  function calculate() {
  const width = window.innerWidth;
  const newInfo = {} as ResponsiveInfo;
  @@ -56,9 +53,14 @@ export function configResponsive(config: ResponsiveConfig) {
  }
  export function useResponsive() {
  - init();
  + if (!listening) {
  + info = {};
  + calculate();
  + window.addEventListener('resize', handleResize);
  + listening = true;
  + }
  const [state, setState] = useState<ResponsiveInfo>(info);
  - const windowExists = typeof window !== "undefined";
  + const windowExists = typeof window !== 'undefined';
  useEffect(() => {
  if (!windowExists) return;
  @@ -69,8 +71,12 @@ export function useResponsive() {
  subscribers.add(subscriber);
  return () => {
  subscribers.delete(subscriber);
  + if (subscribers.size === 0) {
  + window.removeEventListener('resize', handleResize);
  + listening = false;
  + }
  };
  - }, [windowExists]);
  + }, []);
  return state;
  }
  