import { useEffect, useRef } from "react";
/**
 * This hook gets called only when the dependencies change but not during initial render.
 * @param effect - EffectCallback The `useEffect` callback function.
 * @param deps - DependencyList An array of dependencies.
 *
 * @example
 * ```
 *  useNonInitialEffect(()=>{
 *      alert("Dependency changed!");
 * },[dependency]);
 * ```
 */

export default function useNonInitialEffect(effect, deps) {
  if (!effect) {
    return;
  }

  const initialRender = useRef(true);
  useEffect(() => {
    let effectReturns = () => {};

    if (initialRender.current) {
      initialRender.current = false;
    } else {
      effectReturns = effect();
    }

    if (effectReturns && typeof effectReturns === "function") {
      return effectReturns;
    }
  }, deps);
}
;