| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.useUnmountEffect = void 0; |
| 4 |
const react_1 = require("react"); |
| 5 |
/** |
| 6 |
* A `useEffect`-like hook that only triggers when a component unmounts. Does not require a dependency list, as the effect callback will always be kept up to date. |
| 7 |
*/ |
| 8 |
function useUnmountEffect(effect) { |
| 9 |
// Always use the latest effect callback so that it can reference the latest props and state. |
| 10 |
const effectRef = (0, react_1.useRef)(effect); |
| 11 |
effectRef.current = effect; |
| 12 |
// Trigger the effect callback when the component unmounts. |
| 13 |
(0, react_1.useEffect)(() => () => { |
| 14 |
effectRef.current(); |
| 15 |
}, []); |
| 16 |
} |
| 17 |
exports.useUnmountEffect = useUnmountEffect; |
| 18 |
//# sourceMappingURL=useUnmountEffect.js.map |