Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

551 B
Raw Blame
import { useRef, useInsertionEffect, useCallback } from "react";

const useSafeInsertionEffect =
  typeof window !== "undefined"
    ? useInsertionEffect
    : (fn: () => void) => {
        fn();
      };

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export const useEffectEvent = <T extends Function>(callback?: T) => {
  const ref = useRef(callback);

  useSafeInsertionEffect(() => {
    ref.current = callback;
  });

  return useCallback<T>(
    ((...args: unknown[]) => ref.current?.(...args)) as never,
    []
  );
};