-
-
Notifications
You must be signed in to change notification settings - Fork 794
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(regression): updated keyboard handling reaction (by @yusufyildirim) #979
Conversation
@yusufyildirim thanks for diving deep investigating this bug, and it is a regression ( my bad 🤦♂️ ). However your propose solution will trigger the reaction when the derived value changes, this may cause duplicate calls and other issues later on. Thanks for your investigation, i have converted the @yusufyildirim could you please test my latest commit and confirm if it fixes the initial issue, thanks ! |
I did test that, wouldn't propose it as a solution if it was the case 😄 That's what I was trying to explain by saying;
It doesn't mean it won't happen, though. I'll check your commit when I find some time. Thanks! |
@gorhom Tested. It works! |
thanks @yusufyildirim ! |
Not Work corretly for me :( |
1 similar comment
Not Work corretly for me :( |
This PR fixes #946.
Motivation
Looks like a regression is introduced with the PR #931. With that PR,
getKeyboardHeightInContainer
renamed toanimatedKeyboardHeightInContainer
and started usinguseDerivedValue
instead ofuseWorkletCallback
which made it aSharedValue
instead of afunction
to fix another bug. Yet introduced #946, a tricky bug. Here's the screen recording:Screen.Recording.2022-06-09.at.13.12.09.mov
Here's where I placed the
console.log
s to debug:animatedKeyboardHeightInContainer
recalculated: https://github.com/yusufyildirim/react-native-bottom-sheet/blob/fix/keyboard-handling/src/components/bottomSheet/BottomSheet.tsx#L309Why it's happening?
There's OnKeyboardStateChange reaction that reacts to keyboard height and state changes to push the bottom sheet up when necessary. It calls getNextPosition function (right here) to understand where the bottom sheet should be placed so it can animate it to that position. So far so good.
getNextPosition
function depends onanimatedKeyboardHeightInContainer
value --used to begetKeyboardHeightInContainer
-- make the calculation.animatedKeyboardHeightInContainer
derives itself fromanimatedKeyboardHeight
which is pretty much the keyboard height value. This is where the bug is.If you check the screen recording above carefully, you'll see on the second line that the keyboard height value is immediately set. But yet, on the third line,
animatedKeyboardHeightInContainer
value is0
when it's accessed within thegetNextPosition
function. I saidanimatedKeyboardHeightInContainer
is derived from the keyboard height, so "how's this possible?" you might ask. The answer is on the fifth line,animatedKeyboardHeightInContainer
value is being recalculated afterOnKeyboardStateChange
reaction andgetNextPosition
function are called.It used to be not a problem because
getNextPosition
was usinggetKeyboardHeightInContainer
which was a synchronous function call.getKeyboardHeightInContainer
was accessing the latest keyboard height value which was already set at the time it's called. Since it's replaced withanimatedKeyboardHeightInContainer
, a derived state, whenever we access it, we get the latest memoized result. If it's not recalculated before we call it, we don't actually get the latest state.To fix this problem, I added
animatedKeyboardHeightInContainer
to theOnKeyboardStateChange
reaction as an input hoping it'll be re-triggered even ifanimatedKeyboardHeightInContainer
calculation occurs later like it happens in our case. But, while I was waiting for a re-trigger, it never happened.animatedKeyboardHeightInContainer
started to be calculated before, all the time, which is good. I believereanimated
running some sort of dependency analysis algorithm that started to understand it should recalculateanimatedKeyboardHeightInContainer
before calling theOnKeyboardStateChange
.Quite a story for a one-line change but I think it is super important to explain. I hope it helps!