Dashboard sipadu mbip
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

useTimeout.js 652B

1234567891011121314151617181920212223242526
  1. import { useRef } from 'react';
  2. import useWillUnmount from './useWillUnmount';
  3. import useMounted from './useMounted';
  4. /**
  5. * Returns a controller object for setting a timeout that is properly cleaned up
  6. * once the component unmounts. New timeouts cancel and replace existing ones.
  7. */
  8. export default function useTimeout() {
  9. var isMounted = useMounted();
  10. var handle = useRef();
  11. var clear = function clear() {
  12. return clearTimeout(handle.current);
  13. };
  14. useWillUnmount(clear);
  15. return {
  16. set: function set(fn, ms) {
  17. if (!isMounted()) return;
  18. clear();
  19. handle.current = setTimeout(fn, ms);
  20. },
  21. clear: clear
  22. };
  23. }