ndresx / react-countdown

A customizable countdown component for React.
MIT License
746 stars 69 forks source link

[Typescript] How to type ref ? #202

Closed GautierT closed 2 years ago

GautierT commented 2 years ago

Hi.

I'm using typescript and i need to type the countdownRef

const countdownRef = useRef();

<Countdown
        ref={countdownRef}
        date={1000}
/>

What should I do ? Thanks.

image

carlosriveroib commented 2 years ago

here you have the answer :)

Captura de Pantalla 2022-07-05 a las 16 56 39

GautierT commented 2 years ago

Thanks a lot !

All I was doing wrong was not explicitly including "null" as the parameter in the useRef initialization (it expects null, not undefined).

https://stackoverflow.com/a/69143200/2633092

carlosriveroib commented 2 years ago

If you create a ref with undefined const ref = useRef<HTMLDivElement> = useRef() the returned type is MutableRefObject<HTMLDivElement | undefined>

If you create a ref with null const ref = useRef<HTMLDivElement> = useRef(null) the returned type is RefObject

that was the problem

ndresx commented 2 years ago

Thank you!