All files / src/hooks customReactCore.ts

94.44% Statements 17/18
75% Branches 6/8
83.33% Functions 5/6
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                120x       641x   641x 233x 119x 119x 114x             120x 107x   107x 1x 1x     107x 29x 1x         107x    
import {
  useRef,
  useEffect,
  useState,
  DependencyList,
  EffectCallback,
} from "react";
 
export const useStateEffect = (
  effect: EffectCallback,
  deps?: DependencyList,
) => {
  const isMounted = useRef(false);
 
  useEffect(() => {
    if (!isMounted.current) {
      isMounted.current = true;
      return;
    } else return effect();
  }, deps);
};
 
type ActionCallback = (data?: any) => Promise<any | void>;
type TriggerAction = (callback?: ActionCallback, data?: any) => void;
 
export const useAsyncAction = (): [boolean, TriggerAction] => {
  const [isLoading, setIsLoading] = useState(false);
 
  const triggerAction = (callback?: ActionCallback, data?: any) => {
    setIsLoading(true);
    Eif (callback) data ? callback(data) : callback();
  };
 
  useEffect(() => {
    if (isLoading)
      setTimeout(() => {
        setIsLoading(false);
      }, 600);
  }, [isLoading]);
 
  return [isLoading, triggerAction];
};