You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Compiler Bug]: Memoizing a debounced function complains about ref access #31290
Attempting to memoize a debounced function that access a ref gives me this react compiler lint violation:
Ref values (the current property) may not be accessed during render.
However, the underlying function isn't called during render, it's simply referenced, and only called when the memoized, debounced function is called. In my case, it's called in an event handler, which is safe.
What's happening here is that the compiler sees that _scrollToBottom is passed as an argument to a function (in this case debounce()). The compiler doesn't know what debounce does with that function, and has to assume that the function might immediately call its argument — might immediately call _scrollToBottom — which would access a ref during render. So we report an error.
One option would be to try to recognize common functions like debounce, which are known to return but not call their arguments.
Yeah, that's more or less what I figured. Debounce in particular seems like a common enough pattern that it would be worth a special case? Currently I'm just suppressing the error, and I'm not actually using the compiler yet, but I'm curious, what would the compiler do here? I'm guessing it also detects this and bails out of optimization on this component?
I might be misunderstanding the example but why do you call debounce in a useMemo? What's the return value and what are you doing with it? Isn't it useEffect that you want?
debounce is a higher order function, it returns a new function that has the debounce behavior. Since we pass it as a prop, we don't want to create a new reference every render.
Activity
josephsavona commentedon Oct 18, 2024
Thanks for reporting! We'll have to think about how to handle this case.
What's happening here is that the compiler sees that
_scrollToBottomis passed as an argument to a function (in this casedebounce()). The compiler doesn't know what debounce does with that function, and has to assume that the function might immediately call its argument — might immediately call_scrollToBottom— which would access a ref during render. So we report an error.One option would be to try to recognize common functions like debounce, which are known to return but not call their arguments.
RobinClowers commentedon Oct 18, 2024
Yeah, that's more or less what I figured. Debounce in particular seems like a common enough pattern that it would be worth a special case? Currently I'm just suppressing the error, and I'm not actually using the compiler yet, but I'm curious, what would the compiler do here? I'm guessing it also detects this and bails out of optimization on this component?
josephsavona commentedon Oct 19, 2024
Exactly, the compiler would skip memoizing the component.
guillaumebrunerie commentedon Oct 22, 2024
I might be misunderstanding the example but why do you call
debouncein auseMemo? What's the return value and what are you doing with it? Isn't ituseEffectthat you want?RobinClowers commentedon Oct 22, 2024
debounceis a higher order function, it returns a new function that has the debounce behavior. Since we pass it as a prop, we don't want to create a new reference every render.SimenB commentedon Oct 3, 2025
This is now warned about with the new
react-hooks/refsrule which I guess is just the same error? Are there any plans to address this?