react-use-precision-timer
react-use-precision-timer copied to clipboard
Getting access to the timer instance inside the callback
Is this possible? I was hoping it would get passed in as a parameter to the callback but this isn't the case.
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
}, [])