-
-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: replace useEffect with top level ref setting in useStableCallback #2010
Conversation
useEffect(() => { | ||
callbackRef.current = callback; | ||
return () => { | ||
callbackRef.current = undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need the effect at least for the clean up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly do you need to clean the callback up for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callbackRef.current = undefined;
freeing the ref object upon unmounting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. This is the first time I read anything about a need of cleaning up a ref object in a useEffect cleanup function. What advantages does it have?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, according to ChatGPT:
Without a cleanup function, callbackRef.current is never cleared. This might lead to unexpected issues if there are asynchronous tasks that outlive the component lifecycle, as callbackRef may retain a reference to the function even after the component has unmounted. This can cause memory leaks or even runtime errors if asynchronous code tries to call callbackRef.current after unmounting.
I will modify the pull request, thank you.
@gorhom FYI, I can confirm that this diff fixes the issue of the sheet not appearing with dynamic sizing enabled on new architecture. This might also fix #2015 & #2020 To recap, this is the version of the callback that works in my case.
|
This is not totally correct in my opinion, because every time the callback changes, it might be called before the ref is updated. Do we actually need this useStableCallback hook? I believe that the easiest way to fix those issues is to get rid of it in sake of simplicity. |
thank you @pavel-krasnov for submitting this PR <3 |
It seems that this fixed the collapsing of the BottomSheet, but I might be wrong. Why did adding an inset and updating to version 5.06 solve the issue of the BottomSheet closing immediately after opening? I don’t understand how this could be related. Why would the inset affect the BottomSheet closing? A value of 0 doesn’t work, but any non-zero value seems to fix the collapsing issue, but at 5.05 even inset not helps |
I don’t think this is the correct fix. Instead, try supplying the initial value to the ref in the useRef call. For future renders, applying it in an effect is more correct. |
Motivation
Using
useEffect
to update the ref value causes potential issues with calling the callback before the firstuseEffect
invocation. This happens for example in apps created with New Architecture. Setting the callback in the top level of the hook ensures that it is immediately ready to be called.