react-dev-inspector
Version:
dev-tool for inspect react components and jump to local IDE for component code.
30 lines (29 loc) • 1.09 kB
JavaScript
;
/**
* 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.useEffectEvent = void 0;
const react_1 = require("react");
const useEffectEvent = (callback) => {
const callbackRef = (0, react_1.useRef)(callback);
/**
* same as modify ref value in `useEffect`, use for avoid tear of layout update
*/
callbackRef.current = (0, react_1.useMemo)(() => callback, [callback]);
const stableRef = (0, react_1.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;
};
exports.useEffectEvent = useEffectEvent;