Skip to content
Discussion options

You must be logged in to vote

You don't need useCallback dependencies or refs here: values read inside the callback don't go stale. useThrottler creates the Throttler instance once, but reassigns the function on every render (packages/react-pacer/src/throttler/useThrottler.ts):

const [throttler] = useState(() => new Throttler(fn, mergedOptions))

throttler.fn = fn
throttler.setOptions(mergedOptions)

and the core Throttler calls this.fn(...args) at the moment it executes (packages/pacer/src/throttler.ts, #execute), including the trailing call that fires from the timeout. So whatever fn the latest render passed in is the one that runs, closing over that render's state and props. useThrottledCallback is just useThrottler…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@marcoms
Comment options

Answer selected by marcoms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants