react-use-precision-timer icon indicating copy to clipboard operation
react-use-precision-timer copied to clipboard

Getting access to the timer instance inside the callback

Open jamesmoss opened this issue 1 year ago • 1 comments

Is this possible? I was hoping it would get passed in as a parameter to the callback but this isn't the case.

jamesmoss avatar Oct 28 '24 21:10 jamesmoss

Not a React expert - I used useRef and it seems to work:

  const timerRef = useRef<Timer | null>(null)
  const callback = useCallback(() => {
    if (!timerRef.current) return

    const timer = timerRef.current

    // your code
  }, [])

  timerRef.current = useTimer({
    delay: 1000,
  }, callback)

   useEffect(() => {
    return () => timerRef.current = null
  }, [])

stupidSheep avatar Feb 13 '25 08:02 stupidSheep