UNPKG

react-dev-inspector

Version:

dev-tool for inspect react components and jump to local IDE for component code.

26 lines (25 loc) 918 B
/** * Simple but not robust implement of React18 experimental hook `useEffectEvent`, * to keep compatible with other React versions. * * for some more robust implements, you can see: * - `useEvent` in https://github.com/scottrippey/react-use-event-hook * - `useMemoizedFn` in https://github.com/alibaba/hooks */ import { useMemo, useRef } from 'react'; export const useEffectEvent = (callback) => { const callbackRef = useRef(callback); /** * same as modify ref value in `useEffect`, use for avoid tear of layout update */ callbackRef.current = useMemo(() => callback, [callback]); const stableRef = useRef(); // init once if (!stableRef.current) { stableRef.current = (function (...args) { var _a; return (_a = callbackRef.current) === null || _a === void 0 ? void 0 : _a.apply(this, args); }); } return stableRef.current; };