lundi 8 juin 2020

How Can I test my custom Hook in React JS?

How Can I test my custom Hook? Thank you for your help.

import { useEffect, useRef } from 'react';

export function useInterval(callback, delay) {
  const savedCallback = useRef();
  // Remember the latest callback.
  useEffect(() => {
    savedCallback.current = callback;
  }, [callback]);

  // Set up the interval.
  useEffect(() => {
    function tick() {
      savedCallback.current();
    }
    if (delay !== null) {
      const id = setInterval(tick, delay);
      return () => {
        clearInterval(id);
      };
    }
  }, [delay]);
}

In my component I am using it as a timer - perform an action after time declared in a dependency

Aucun commentaire:

Enregistrer un commentaire