react-native icon indicating copy to clipboard operation
react-native copied to clipboard

`KeyboardAvoidingView` infinitely updates on Android

Open winghouchan opened this issue 1 year ago • 2 comments

Description

The bottom state of the KeyboardAvoidingView component infinitely updates on Android. This is visible when inspecting KeyboardAvoidingView's state in React Native DevTools, it continuously changes value. Or when a component is rendered just above the keyboard, it flickers and/or bounces up and down.

Steps to reproduce

Prerequisites

  1. App from reproducer repo is built and open. Reproduction 1 is on branch example-1. Reproduction 2 is on branch example-2. For each branch, checkout, yarn install then yarn android.
  2. React Native DevTools is running.

Reproduction 1

Reproduction 1 is a text input and footer fixed to the bottom wrapped in a KeyboardAvoidingView. While the bottom state changes, the change is so small (<1 px) that it is not noticeable in the UI.

Steps
  1. Inspect KeyboardAvoidingView in the React Native DevTools.
  2. Focus on text input in app.
  3. Check bottom state in DevTools.
  4. Unfocus text input in app.
  5. Check bottom state in DevTools.
Expected behaviour

The bottom state should not continuously change.

Actual behaviour

The bottom state continuously changes.

Reproduction 2

Reproduction 2 is the same as reproduction 1 but with two screens to show the effect on the UI as the bottom state change is much larger.

Steps
  1. Focus on text input in the app.
  2. Tap "Go to screen 2".
  3. Inspect Screen2's KeyboardAvoidingView in the React Native DevTools.
Expected behaviour

The bottom state should not continuously change. The footer should not be flickering.

Actual behaviour

The bottom state continuously changes. The footer is flickering.

React Native Version

0.74.2

Affected Platforms

Runtime - Android

Output of npx react-native info

System:
  OS: macOS 14.5
  CPU: (8) arm64 Apple M1
  Memory: 100.70 MB / 16.00 GB
  Shell:
    version: 3.7.0
    path: /opt/homebrew/bin/fish
Binaries:
  Node:
    version: 20.11.1
    path: ~/.local/share/mise/installs/node/20/bin/node
  Yarn:
    version: 3.6.4
    path: ~/.local/share/mise/installs/node/20/bin/yarn
  npm:
    version: 10.2.4
    path: ~/.local/share/mise/installs/node/20/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.5
      - iOS 17.5
      - macOS 14.5
      - tvOS 17.5
      - visionOS 1.2
      - watchOS 10.5
  Android SDK:
    API Levels:
      - "33"
      - "34"
    Build Tools:
      - 29.0.2
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 33.0.1
      - 34.0.0
    System Images:
      - android-33 | Google APIs ARM 64 v8a
      - android-34 | ARM 64 v8a
      - android-34 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2023.2 AI-232.10227.8.2321.11479570
  Xcode:
    version: 15.4/15F31d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /opt/homebrew/opt/openjdk@17/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.74.2
    wanted: 0.74.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Stacktrace or Logs

N/A

Reproducer

https://github.com/winghouchan/react-native-keyboardavoidingview-infinite-update-mcve

Screenshots and Videos

Reproduction 1

https://github.com/facebook/react-native/assets/6061021/4c89d653-2030-4a65-bf47-8a2ec895fd39

Reproduction 2

https://github.com/facebook/react-native/assets/6061021/43f2a203-232a-425d-a537-91f4f96ea8b5

winghouchan avatar Jun 26 '24 20:06 winghouchan

Potentially related issues:

  • #30495
  • #30532

winghouchan avatar Jun 26 '24 20:06 winghouchan

Hmm, I don't know if this is somehow connected, but after upgrading from 0.73^ to 0.74^, the windowSoftInputMode / KeyboardAvoidingView has stopped working correctly on Android:

RN 0.73^

https://github.com/facebook/react-native/assets/16654786/67e0a7c1-816d-4c24-a06e-39072e020918

RN 0.74^

https://github.com/facebook/react-native/assets/16654786/557901c5-4dfb-44ad-8ac2-a26ead77945b

Looks like serious issue, but need more time to make clean repro.

ahanusek avatar Jul 10 '24 21:07 ahanusek

This is an issue for our app also - the fact that the Android keyboard seems to have a continuously-changing height. This is causing a custom Modal that we have in our app which repositions itself based upon a combination of the height of the keyboard and whether it is visible. Works fine on iOS (because the keyboard height is constant). But, because the Android keyboard height constantly changes, our app crashes with this error:

Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

This is an excerpt of the code we are using to track the keyboard height so we can reposition the modal (by kicking off an animation on the bottom margin of the modal to matching the height of the keyboard, or 0 if the keyboard is not visible).

    const [_isKeyboardVisible, setKeyboardVisible] = useState(false)
    const [_keyboardHeight, setKeyboardHeight] = useState(0)

    useEffect(() => {
        const showSubscription = Keyboard.addListener('keyboardDidShow', e => _updateKeyboardData(e, true))
        const hideSubscription = Keyboard.addListener('keyboardDidHide', e => _updateKeyboardData(e, false))
    
        return () => {
          showSubscription.remove()
          hideSubscription.remove()
        }
    }, [])

    const _updateKeyboardData = (e, isVisible) => {
        setKeyboardHeight(isVisible ? e.endCoordinates?.height : 0)
        setKeyboardVisible(isVisible)
    }

gtwilliams03 avatar Jul 17 '24 19:07 gtwilliams03

Sorry - I forgot to mention that the issue might be with the actual height of the Keyboard changing as opposed to the KeyboardAvoidingView component, if that helps at all.

gtwilliams03 avatar Jul 17 '24 19:07 gtwilliams03

I also encountered the same problem on 0.75.3.It makes the bottom of my screen flicker :(

NiuGuohui avatar Sep 26 '24 06:09 NiuGuohui

Might be related to https://github.com/facebook/react-native/pull/45928, since it seems to be working fine in 0.75.1 and this change was introduced in the .2 version and has something to do with the KeyboardAvoidingView.

The issue is also still present in 0.75.4

ChielBruin avatar Oct 14 '24 09:10 ChielBruin

This problem still exists after I upgrade to 0.76.

NiuGuohui avatar Nov 20 '24 01:11 NiuGuohui

I'm having this exact same issue. Is there any solution or workaround there?

https://github.com/user-attachments/assets/537a2d22-3cbd-44ef-a432-3c4aab4f7788

octaviotastico avatar Nov 21 '24 18:11 octaviotastico

I'm having this exact same issue. Is there any solution or workaround there?

Recording.2024-11-21.155350.mp4

Did you find a fix for this?

nemanjamitric avatar Jan 31 '25 16:01 nemanjamitric

I am also facing the same problem after updating from Expo 51 to Expo 52

nop33 avatar Apr 23 '25 13:04 nop33

this might help you guys as a temporary solution 👀 https://mateusz1913.github.io/react-native-avoid-softinput/

egzon-mustafa avatar Apr 30 '25 09:04 egzon-mustafa