I am setting up a useRef object to hold a NodeJS.Timeout component, the return type of setInterval(). When I use that later in clearInterval, I get an error (shown below) on both instances of intervalRef.current in the code.
What is the correct way to set this up so I can call clearInterval in the button onClick event?
function HookTimer(): ReactElement {
const [timer, setTimer] = useState(0)
let intervalRef = useRef<NodeJS.Timeout>()
useEffect(() => {
intervalRef.current = setInterval(() => {
setTimer(prevTimer => prevTimer + 1)
}, 1000)
return () => {
clearInterval(intervalRef.current)
}
}, [])
return (
<div>
Hook Timer - {timer}
<div>
<button onClick={() => clearInterval(intervalRef.current)}>Clear Hook Timer</button>
</div>
</div>
)
No overload matches this call. Overload 1 of 2, '(intervalId: Timeout): void', gave the following error. Argument of type 'Timeout | undefined' is not assignable to parameter of type 'Timeout'. Type 'undefined' is not assignable to type 'Timeout'. Overload 2 of 2, '(handle?: number | undefined): void', gave the following error. Argument of type 'Timeout | undefined' is not assignable to parameter of type 'number | undefined'. Type 'Timeout' is not assignable to type 'number'.ts(2769)