Files
webstudio/apps/builder/app/shared/hook-utils/effect-event.ts
Bogdan Chadkin c28faf27ab build: bump eslint (#4325)
eslint-plugin-react-hooks now works with eslint 9 and do not require
@eslint/compat wrapper.
2024-10-22 17:20:47 +03:00

23 lines
551 B
TypeScript

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,
[]
);
};