SHARE
TWEET

Untitled

a guest Sep 15th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useRef, useEffect } from "react";
  2.  
  3. const Timer = () => {
  4.   const intervalRef = useRef();
  5.  
  6.   useEffect(() => {
  7.     const id = setInterval(() => {
  8.       console.log("Um segundo se passou.");
  9.     }, 1000);
  10.    
  11.     /*
  12.       Tornamos o id do intervalo acessível para todo o componente.
  13.       Se tivéssemos usado uma variável de estado para isso,
  14.       componente seria renderizado novamente e isso causaria um loop infinito.
  15.     */
  16.     intervalRef.current = id;
  17.    
  18.     return () => clearInterval(intervalRef.current);
  19.   });
  20.  
  21.   const handleCancel = () => clearInterval(intervalRef.current);
  22.  
  23.   return (
  24.     <>
  25.       //...
  26.     </>
  27.   );
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top