Skip to content
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

TextInput onContentSizeChange triggers twice inside a Modal #47186

Open
ilaloov opened this issue Oct 24, 2024 · 7 comments · May be fixed by #47492
Open

TextInput onContentSizeChange triggers twice inside a Modal #47186

ilaloov opened this issue Oct 24, 2024 · 7 comments · May be fixed by #47492
Labels
0.76 Component: Modal Component: TextInput Related to the TextInput component. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Newer Patch Available Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)

Comments

@ilaloov
Copy link

ilaloov commented Oct 24, 2024

Description

When using a TextInput inside a Modal, the onContentSizeChange callback is triggered twice instead of once. Outside the Modal the event triggers correctly (only once).

Steps to reproduce

  1. Install and launch the application
  2. Observe that onContentSizeChange triggers once (as expected)
  3. Click on the "Show modal" button
  4. Notice that onContentSizeChange for the TextInput inside the Modal triggers twice (unexpected behavior)

React Native Version

0.76.0

Affected Platforms

Runtime - Android, Runtime - iOS

Output of npx react-native info

System:
  OS: Windows 10 10.0.19045
  CPU: (8) x64 Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
  Memory: 9.86 GB / 31.84 GB
Binaries:
  Node:
    version: 20.12.1
    path: C:\Program Files\nodejs\node.EXE
  Yarn: Not Found
  npm:
    version: 9.8.1
    path: C:\Program Files\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK:
    Android NDK: 22.1.7171670
  Windows SDK: Not Found
IDEs:
  Android Studio: AI-231.9392.1.2311.11330709
  Visual Studio: Not Found
Languages:
  Java: 17.0.8
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.0-alpha.2
    wanted: 15.0.0-alpha.2
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.0
    wanted: 0.76.0
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Stacktrace or Logs

(NOBRIDGE) LOG  39.607845306396484
(NOBRIDGE) LOG  20.39215660095215
(NOBRIDGE) LOG  39.607845306396484

Reproducer

https://github.com/ilaloov/textinput-issue

Screenshots and Videos

No response

@react-native-bot react-native-bot added Component: Modal Component: TextInput Related to the TextInput component. labels Oct 24, 2024
@shubhamguptadream11 shubhamguptadream11 added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed Needs: Triage 🔍 labels Oct 24, 2024
@shubhamguptadream11
Copy link
Collaborator

@ilaloov Is this happening on New architecture as well?

@ilaloov ilaloov closed this as completed Oct 25, 2024
@ilaloov
Copy link
Author

ilaloov commented Oct 25, 2024

@shubhamguptadream11 Yes, both with new architecture enabled and disabled

@ilaloov ilaloov reopened this Oct 25, 2024
@cipolleschi cipolleschi added Newer Patch Available Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules) 0.76 labels Oct 28, 2024
@react-native-bot
Copy link
Collaborator

⚠️ Newer Version of React Native is Available!
ℹ️ You are on a supported minor version, but it looks like there's a newer patch available - undefined. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@coado
Copy link
Collaborator

coado commented Oct 31, 2024

Hey @ilaloov, do you see this issue happening on both Android and iOS? I can see the issue happening on Android but it seems to be fine on iOS.

@ilaloov
Copy link
Author

ilaloov commented Oct 31, 2024

@coado I double-checked on iOS (emulator). It might be the emulator, but it triggers twice as well. However, the returned value (height) is constant, whereas on Android, it varies (20.39215660095215, 39.607845306396484).

@coado
Copy link
Collaborator

coado commented Oct 31, 2024

@ilaloov This is how it works for me on iPhone 16 emulator. The onContentSizeChange in outside TextInput is printed twice initially, but when I open a modal with the inside TextInput it prints only once.

onContentSizeChange.mov
code
import {Button, Modal, SafeAreaView, StyleSheet, TextInput} from 'react-native';
import React, {useState} from 'react';

export default function App() {
  const [modal, setModal] = useState(false);

  return (
    <SafeAreaView style={styles.container}>
      <TextInput
        multiline
        onContentSizeChange={e => console.log("outside: ", e.nativeEvent.contentSize.height)}
        style={styles.textInput}
      />
      <Button onPress={() => setModal(true)} title="Show modal" />
      <Modal visible={modal}>
        <TextInput
          multiline
          onContentSizeChange={e =>
            console.log("inside: ", e.nativeEvent.contentSize.height)
          }
          style={[styles.textInput, styles.textInputModal]}
        />
        <Button onPress={() => setModal(false)} title="Hide modal" />
      </Modal>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#ecf0f1',
    flex: 1,
    justifyContent: 'center',
    padding: 8,
  },
  textInput: {
    backgroundColor: 'gray',
  },
  textInputModal: {
    marginTop: 100,
  },
});

@coado
Copy link
Collaborator

coado commented Nov 5, 2024

I think I found what causes this issue. The problem is that on Android the screen size in modal view state is updated asynchronous which leads to triggering onLayout in ReactEditText multiple times. Setting the screen size before the layout happens similarly to how it is handled on iOS seems to fix it. I will try to open a PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.76 Component: Modal Component: TextInput Related to the TextInput component. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Newer Patch Available Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)
Projects
None yet
5 participants