@react-hookz/web
Version:
React hooks done right, for browser and SSR.
40 lines (39 loc) • 1.62 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLifecycleLogger = void 0;
/* eslint-disable no-console */
var react_1 = require("react");
/**
* This hook provides a console log when the component mounts, updates and unmounts.
*
* @param componentName Provides the name of the component in which the life cycle is being logged
* @param deps Dependencies list, as for `useEffect` hook
*/
function useLifecycleLogger(componentName, deps) {
var mountedRef = (0, react_1.useRef)(false);
(0, react_1.useEffect)(function () {
if (mountedRef.current) {
console.log("".concat(componentName, " updated"), deps && __spreadArray([], deps, true));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
(0, react_1.useEffect)(function () {
mountedRef.current = true;
console.log("".concat(componentName, " mounted"), deps && __spreadArray([], deps, true));
return function () {
mountedRef.current = false;
console.log("".concat(componentName, " unmounted"));
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}
exports.useLifecycleLogger = useLifecycleLogger;