Yo uso este código:
const clearTimer = (e: any) => { setTimer(<div></div>); if (Ref.current) clearInterval(Ref.current); startTimer(e); const id = setInterval(() => { startTimer(e); }, 1000) Ref.current = id; } Para Ref.current = id; me sale este error:
(property) MutableRefObject<null>.current: null Type 'Timer' is not assignable to type 'null'.Y estas líneas no resolvieron mi problema:
Ref.current = id.toString(); Ref.current = id ?? '';Al usar useRef con Typescript, debe proporcionar un argumento genérico para especificar cuál es el tipo esperado del valor retenido.
En tu caso:
// The `current` value is expected to be a `Timer`, and is initialized to `null` const Ref = useRef<Timer>(null); Entonces Ref.current se puede asignar al resultado de setInterval .
Consulte este artículo para obtener más información: https://linguinecode.com/post/how-to-use-react-useref-with-typescript