mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-10-29 08:07:48 +09:00
eslint-plugin-react-hooks now works with eslint 9 and do not require @eslint/compat wrapper.
23 lines
551 B
TypeScript
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,
|
|
[]
|
|
);
|
|
};
|