useRef TypeScript – not assignable to type LegacyRef

None of the above worked for me, but turns out the solution was quite simple…

All I was doing wrong was not explicitly including “null” as the parameter in the useRef initialization (it expects null, not undefined).
Also you CANNOT use “HTMLElement” as your ref type, you have to be more specific, so for me it was “HTMLDivElement” for example).

So working code for me was something like this:

const ref = useRef<HTMLDivElement>(null);

return <div ref={ref}> Some Content... </div>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)