I use this code:
const clearTimer = (e: any) => {
setTimer(<div></div>);
if (Ref.current) clearInterval(Ref.current);
startTimer(e);
const id = setInterval(() => {
startTimer(e);
}, 1000)
Ref.current = id;
}
For Ref.current = id; I get this error:
(property) MutableRefObject<null>.current: null
Type 'Timer' is not assignable to type 'null'.
And these lines not solved my problem:
Ref.current = id.toString();
Ref.current = id ?? '';
Ref.currentis typed to only accept the valuenull. So you can't assignRef.currentto be aTimerright now. Can you post howRefis initialized? If you can fix howRefis typed such that it is aMutableRefObject<Timer>, then you should be able to assign aTimerto it.const Ref = useRef(null);I change it touseRef(MutableRefObject<String>)but not work yet @DavidShortmanuseRefmay be initialized as null, but you can inform it what type it is expected to be set to by providing a generic:const Ref = useRef<Timer | null>(null);