New Jun 2, 2026

How do I use useRef as input in a function with TypeScript?

Libraries, Frameworks, etc. All from Newest questions tagged reactjs - Stack Overflow View How do I use useRef as input in a function with TypeScript? on stackoverflow.com

I am trying to make an automatic scroll preference in production environments. For the scrolling utility for my React website I have chosen TypeScript due to what I have heard about its ability to catch issues earlier on, however I have no idea what type to put on my ref element in the scrollToSection() function. No article I found covered this exact use case, for instance nulldog and xjavascript.

function Navbar() {
  const section1 = useRef<HTMLElement>(null)
  const section2 = useRef<HTMLElement>(null)
  const section3 = useRef<HTMLElement>(null)
  const section4 = useRef<HTMLElement>(null)

const scrollToSection = (ref) => { ref.current?.scrollIntoView({ behaviour: 'smooth' }) }

return ( <nav> <NavLink to='/wiki'>Wiki</NavLink> </nav> ) }

Scroll to top