Files
webstudio/apps/builder/app/shared/hook-utils/effect-event.ts
T
Bogdan Chadkin 8ed3400167 build: switch to eslint 9 (#3877)
We finally can switch to the latest eslint
with new config format.
2024-08-05 09:23:57 +02:00

24 lines
609 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;
});
// eslint-disable-next-line react-hooks/exhaustive-deps
return useCallback<T>(
((...args: unknown[]) => ref.current?.(...args)) as never,
[]
);
};