react-errorr
Version:
A react popup error fully customizable
34 lines (33 loc) • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const useEffectOnce = (effect) => {
const destroyFunc = (0, react_1.useRef)();
const effectCalled = (0, react_1.useRef)(false);
const renderAfterCalled = (0, react_1.useRef)(false);
const [, setVal] = (0, react_1.useState)(0);
if (effectCalled.current) {
renderAfterCalled.current = true;
}
(0, react_1.useEffect)(() => {
// only execute the effect first time around
if (!effectCalled.current) {
destroyFunc.current = effect();
effectCalled.current = true;
}
// this forces one render after the effect is run
setVal((val) => val + 1);
return () => {
// if the comp didn't render since the useEffect was called,
// we know it's the dummy React cycle
if (!renderAfterCalled.current) {
return;
}
if (destroyFunc.current) {
destroyFunc.current();
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
};
exports.default = useEffectOnce;