Hey Murali,
Thanks for chipping in. You’re correct. Moving the throttled function outside the component will solve the issue. I’m sure some might find this solution simpler and adequate. My goal here was to make it work inside the functional component.
Traditionally we’ve used refs for DOM references. useRef() hook provides you more than that. Here is a quote from the React site:
However,
useRef()
is useful for more than theref
attribute. It’s handy for keeping any mutable value around similar to how you’d use instance fields in classes.This works because
useRef()
creates a plain JavaScript object. The only difference betweenuseRef()
and creating a{current: ...}
object yourself is thatuseRef
will give you the same ref object on every render.
Cheers.