UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

36 lines (35 loc) 1.44 kB
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)); }; /* eslint-disable no-console */ import { useEffect, useRef } from '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 */ export function useLifecycleLogger(componentName, deps) { var mountedRef = useRef(false); useEffect(function () { if (mountedRef.current) { console.log("".concat(componentName, " updated"), deps && __spreadArray([], deps, true)); } // eslint-disable-next-line react-hooks/exhaustive-deps }, deps); 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 }, []); }