react-native: [iOS][KeyboardAvoidingView] Using KeyboardAvoidingView inside a ScrollView causes the app the crash when focusing an input
Description
When KeyboardAvoidingView inside a ScrollView and focus a TextInput the app crashes (goes extremely slow).
This seems to only happen if the KeyboardAvoidingView needs to trigger the animation, if the ScrollView is small it works fine.
Testing with react native 0.72.9 the issue doesn’t seem to happen
Steps to reproduce
- install the app
yarn ios - Using the simulator make sure the keyboard shows when entering a text input
- Focus a text input and start typing on the keyboard
- Dismiss the keyboard / click outside of the input
- You will notice the app has now hung
React Native Version
0.73.4
Affected Platforms
Runtime - iOS
Output of npx react-native info
System:
OS: macOS 14.2.1
CPU: (10) arm64 Apple M1 Max
Memory: 127.84 MB / 32.00 GB
Shell:
version: "5.9"
path: /bin/zsh
Binaries:
Node:
version: 18.18.2
path: ~/.nvm/versions/node/v18.18.2/bin/node
Yarn:
version: 1.22.21
path: ~/.nvm/versions/node/v18.18.2/bin/yarn
npm:
version: 9.8.1
path: ~/.nvm/versions/node/v18.18.2/bin/npm
Watchman: Not Found
Managers:
CocoaPods:
version: 1.14.3
path: /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 23.2
- iOS 17.2
- macOS 14.2
- tvOS 17.2
- visionOS 1.0
- watchOS 10.2
Android SDK: Not Found
IDEs:
Android Studio: 2021.1 AI-211.7628.21.2111.8309675
Xcode:
version: 15.2/15C500b
path: /usr/bin/xcodebuild
Languages:
Java:
version: 17.0.9
path: /usr/bin/javac
Ruby:
version: 2.6.10
path: /usr/bin/ruby
npmPackages:
"@react-native-community/cli": Not Found
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.73.4
wanted: 0.73.4
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: false
iOS:
hermesEnabled: true
newArchEnabled: false
### Stacktrace or Logs
```text
Excessive number of pending callbacks: 501. Some pending callbacks that might have leaked by never being called from native code: {"235":{"module":"UIManager","method":"configureNextLayoutAnimation"},"238":{"module":"UIManager","method":"configureNextLayoutAnimation"},"241":{"module":"UIManager","method":"configureNextLayoutAnimation"},"244":{"module":"UIManager","method":"configureNextLayoutAnimation"},"247":{"module":"UIManager","method":"configureNextLayoutAnimation"},"250":{"module":"UIManager","method":"configureNextLayoutAnimation"},"253":{"module":"UIManager","method":"configureNextLayoutAnimation"},"256":{"module":"UIManager","method":"configureNextLayoutAnimation"},"259":{"module":"UIManager","method":"configureNextLayoutAnimation"},"262":{"module":"UIManager","method":"configureNextLayoutAnimation"},"265":{"module":"UIManager","method":"configureNextLayoutAnimation"},"268":{"module":"UIManager","method":"configureNextLayoutAnimation"},"271":{"module":"UIManager","method":"configureNextLayoutAnimation"},"274":{"module":"UIManager","method":"configureNextLayoutAnimation"},"277":{"module":"UIManager","method":"configureNextLayoutAnimation"},"280":{"module":"UIManager","method":"configureNextLayoutAnimation"},"283":{"module":"UIManager","method":"configureNextLayoutAnimation"},"286":{"module":"UIManager","method":"configureNextLayoutAnimation"},"289":{"module":"UIManager","method":"configureNextLayoutAnimation"},"292":{"module":"UIManager","method":"configureNextLayoutAnimation"},"295":{"module":"UIManager","method":"configureNextLayoutAnimation"},"298":{"module":"UIManager","method":"configureNextLayoutAnimation"},"301":{"module":"UIManager","method":"configureNextLayoutAnimation"},"304":{"module":"UIManager","method":"configureNextLayoutAnimation"},"307":{"module":"UIManager","method":"configureNextLayoutAnimation"},"310":{"module":"UIManager","method":"configureNextLayoutAnimation"},"313":{"module":"UIManager","method":"configureNextLayoutAnimation"},"316":{"module":"UIManager","method":"configureNextLayoutAnimation"},"319":{"module":"UIManager","method":"configureNextLayoutAnimation"},"322":{"module":"UIManager","method":"configureNextLayoutAnimation"},"325":{"module":"UIManager","method":"configureNextLayoutAnimation"},"328":{"module":"UIManager","method":"configureNextLayoutAnimation"},"331":{"module":"UIManager","method":"configureNextLayoutAnimation"},"334":{"module":"UIManager","method":"configureNextLayoutAnimation"},"337":{"module":"UIManager","method":"configureNextLayoutAnimation"},"340":{"module":"UIManager","method":"configureNextLayoutAnimation"},"343":{"module":"UIManager","method":"configureNextLayoutAnimation"},"346":{"module":"UIManager","method":"configureNextLayoutAnimation"},"349":{"module":"UIManager","method":"configureNextLayoutAnimation"},"352":{"module":"UIManager","method":"configureNextLayoutAnimation"},"355":{"module":"UIManager","method":"configureNextLayoutAnimation"},"358":{"module":"UIManager","method":"configureNextLayoutAnimation"},"361":{"module":"UIManager","method":"configureNextLayoutAnimation"},"364":{"module":"UIManager","method":"configureNextLayoutAnimation"},"367":{"module":"UIManager","method":"configureNextLayoutAnimation"},"370":{"module":"UIManager","method":"configureNextLayoutAnimation"},"373":{"module":"UIManager","method":"configureNextLayoutAnimation"},"376":{"module":"UIManager","method":"configureNextLayoutAnimation"},"379":{"module":"UIManager","method":"configureNextLayoutAnimation"},"382":{"module":"UIManager","method":"configureNextLayoutAnimation"},"...(truncated keys)...":451}
### Reproducer
https://github.com/slauzinho/KeyboardAvoidingViewExample
### Screenshots and Videos
https://github.com/facebook/react-native/assets/445345/72aa333e-f55a-446f-aea5-6b7615b8a4ca
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 30
- Comments: 61 (16 by maintainers)
Commits related to this issue
- add flex to keyboard view to avoid hang https://github.com/facebook/react-native/issues/42939 — committed to Helpful-Consultants/PocketInfowebSDK46 by alanupstone 3 years ago
move
ScrollViewinsideKeyboardAvoidingView, it works for meI’m looking into this, and I see that the app get blocked and eventually crashes. However, this bug is only happening in the Old Architecture, so sadly I can’t spend more time on this as we should focus on the New Architecture.
To unblock on this, I actually think that you should not use the
<KeyboardAvoidingView>component inside a scrollview. If what you want to scroll is the scrollview, the<ScrollView>component has the propautomaticallyAdjustKeyboardInsetsthat should fix the issue on the Old Architecture. It should also work in the New Architecture.@cortinico same issue.
Moving KeyboardAvoidingView over a ScrollView fixed my problem with crashing application when clicking on an input (IOS)
The crash was happening when publishing the app and on real device.
Guideline 2.1 - Performance - App Completeness
We discovered one or more bugs in your app. Specifically, login screen was unresponsive. Please review the details below and complete the next steps.
Review device details:
A great reason to migrate to the New Architecture! 🤣
Jokes aside, thanks for the clarification, I’ll try and investigate it further. I’m reopening the issue.
Been debugging an issue with our app freezing on certain screens only in real iOS 17.5+ devices, and finally narrowed it down to this same issue here that people have been reporting.
Some details:
@cipolleschi while I understand the prioritization of the new architecture right now, I’d argue that there are probably a good number of teams and products still using older versions of RN. Even for those of us who are on the newer versions where we can opt into the new architecture, there are a lot of third party packages and dependencies that don’t support the new architecture yet, which leaves us in a tough spot because “just upgrade to the new architecture” is not a solution. (And as a few others have also reported, it’s not just a ScrollView / SafeArea issue)
i have same issue on Expo SDK ~52.0.46 and both RN 0.73 and 0.76.9 . also i have not add the newArchEnable: true in app.json
i am share my code app frozen and when reopen the app it will crash in ios
iPhone 15 Pro Max +1iOS 18.5` <SafeAreaView style={[styles.safeArea]}> <TouchableWithoutFeedback onPress={() => { Keyboard.dismiss() }}> <View style={{ flex: 1, paddingHorizontal: 20, }}> <View style={[styles.flex]}> <View style={{ marginTop: 30 }}> <FontAwesome6 name={‘users’} size={48} color={‘#E898FE’} alignSelf={‘center’} /> </View>please help me if any one find the solution : @ftroisi @cipolleschi @mobinni @matinzd @alfonsocj @johnnywang
@cipolleschi In my experience and seemingly others based on their comments in this github issue, this crash/freeze still happens when new architecture is off on both react-native v0.75 and v0.76
I’m facing this same issue too, using
<KeyboardAvoidingView/>inside of a<SafeAreaView/>.It only freezes on a real iOS device (not all iOS devices either, so far I’ve been able to reproduce this issue on iPhone SE running latest iOS) - but unable reproduce on simulator/android.
Update: Flipping the sequence so that SafeAreaView is encapsulated by KeyboardAvoidingView seems to resolve it - confirmed after testing.
“react-native”: “0.73.7”
Experienced the same thing, I had
<KeyboardAvoidingView/>inside of a<SafeAreaView/>. It works on the iOS Emulator, on Android simulator and device, it only “freezes” on real iOS device. I’m following this conversation now.“react-native”: “0.73.6”
Like @kokosky93 , my workaround was to swap the component order to make the
KeyboardAvoidingViewthe parent element of theScrollView, then add a new childViewfor applying container styles that were previously on theKeyboardAvoidingView.So far, everything seems to be working as intended.
I also observed that this issue only affected distributions running on real iOS devices. Not reproducible in either ExpoGo or Android builds.
Try using the new architecture, my crashes vanished after switching
Adding this to KeyboardAvoidingView stops the release bug:
In testing this I did not see any immediate regressions in the behaviour of KBAV
Thanks @mobinni for the stable reproducer. We will definitely have a look at it. To set expectation, I’ll be off for the next week and @cortinico will be back on Thursday, but it will have an higher priority when we are back.
Hi there, I think this issue deserves a lot more attention and a remediation or patch.
We’ve been experiencing this in various places in our app since upgrading. We have a relatively large codebase with various patterns of keyboard avoiding view.
Although swapping to
behavior="padding"ensures that it won’t hang and break on iOS release, what has actually happened is we’ve upgraded to 0.73 is we can’t trust the “platform”Regression testing allows us to catch some of the places, this bug isn’t on all iOS devices, it’s intermittent at best and not everyone is affected so we can’t be confident.
Basically a core pattern that has worked forever in react native is now broken 😅 , we are not ready to move to the new architecture, it was not present on 0.72.+
I’m not sure what has changed between 72 and 73 in the keyboard avoiding view logic for iOS and I am more than happy to help track down and remediate the root cause with some guidance.
I was able to create a stable reproduction here: https://github.com/mobinni/RNBrokenKeyboardAvoidingView
The TLDR is that if you wrap your content in a
Viewwhere the height is set it makes the app hang in release mode on iOS only if the behavior is set to anything butpaddingDigging a bit deeper it seems these lines are what cause the failure in
KeyboardAvoidingView.js:And even deeper down
One thing I noticed is that when I use a regular stack navigator from react native I still get hanging frames but it self-resolves and does not crash, I’m thinking it may have something to do with react-native-screens mounting native elements and the
RCTTextInputComponentViewautoFocus breaking it@cipolleschi We’re seeing this issue too across old and new devices; we’ve just upgraded to
"react-native": "0.73.8"and are only seeing it on ‘real’ ios devices.Similar to https://github.com/facebook/react-native/issues/42939#issuecomment-2071272461, we’re getting it on
SafeAreaView; we’ll see if we can flip it too and see what happens.Hello there, I’m sorry that you are encountering this issue. I see that this is happening with the Old Architecture. Is it happening also with the New Architecture?
At the moment we are hyper focused on rolling out the New Architecture, so issues for the Old Architecture only has lower priority as they will move capacity away from our final goal.
Hi guys
I have the same problem with rn 0.73.9 on iOS real device in production and scheme build config:Release on simulator. It worried me but solved.
If KeyboardAvoidingView inside the ScrollView with prop behavior=“padding” in iOS,screen is freezes. I change behavior padding to position for ios it worked but screen bounced when typing.
Using it belows everything worked for me.
Some said above that it was caused by SafeAreaView. My SafeAreaView defined in App.js. In my project, it’s not because of it.
I hope it’ll fixed.
I was having this issue with Expo SDK 52 and both ReactNative 0.76.9 and 0.77.2. For me the solution was to switch to the new architecture (setting
"newArchEnabled": trueinapp.json)Yes, 0.73 is out of support now. I’m glad that this is fixed in versions > 0.73.
I believe a few other commenters have mentioned that they’re also experiencing this when used inside a SafeAreaView, although I was noticing this happening even without that. I’m not sure if I’ll be able to come up with a repro, but I’ll link here if I do. My guess is that it’s some combination of:
I’ve been able to get around the problem for now by following what others have done and moving over to keyboard-controller’s KAV + using the safe area hook instead, but it’s admittedly a little frustrating since this is an unexpected/unnecessary migration
We’ve encountered this issue on some real iOS devices, despite using both
SafeAreaViewandKeyboardAvoidingViewin our React Native app.To address this, I’m planning to switch to Keyboard Controller and replace
SafeAreaViewwith the corresponding hook. I’ll release an update to see if this resolves the problem.It’s challenging to debug since I haven’t been able to reproduce the issue on my end.
@cipolleschi, could you assist with this? It feels like a regression that’s been around in RN for a while.
Environment:
I think I’m experiencing something similar with TextInput inside of a KeyboardAvoidingView on react native 0.74.1.
We were running into this as well and have temporarily removed the
KeyboardAvoidingViewthat was triggering this.Experiencing the same issue here! RN v0.73.4 as well. The issue indeed seems to be related to being inside a
ScrollView, and on top having the behaviour set topadding, like so:<KeyboardAvoidingView behavior="padding">When setting the behaviour to
position, there is no problem.<KeyboardAvoidingView behavior="position">Edit: added more details
Kindly check this key in scrollview overScrollMode=‘never’ @kokosky93