diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt index 91cc00429008..54a7c8c23a91 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt @@ -1350,12 +1350,14 @@ public open class ReactEditText public constructor(context: Context) : AppCompat FLog.e(TAG, "onTextChanged[$id]: $s $start $before $count") } + // The cached spannable must be up to date before the listeners run: a listener may commit a + // state update synchronously, which measures this view through the cached spannable. + updateCachedSpannable() + if (!isSettingTextFromJS) { listeners?.forEach { listener -> listener.onTextChanged(s, start, before, count) } } - updateCachedSpannable() - onContentSizeChange() } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt index e94f841ffb99..7dce18c61378 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt @@ -12,9 +12,12 @@ import android.text.TextWatcher import com.facebook.react.bridge.ReactContext import com.facebook.react.bridge.WritableMap import com.facebook.react.bridge.WritableNativeMap +import com.facebook.react.common.annotations.UnstableReactNativeAPI +import com.facebook.react.uimanager.StateWrapper import com.facebook.react.uimanager.UIManagerHelper import com.facebook.react.uimanager.events.EventDispatcher +@OptIn(UnstableReactNativeAPI::class) internal class ReactTextInputTextWatcher( reactContext: ReactContext, private val editText: ReactEditText, @@ -52,7 +55,10 @@ internal class ReactTextInputTextWatcher( val newStateData: WritableMap = WritableNativeMap() newStateData.putInt("mostRecentEventCount", editText.incrementAndGetEventCounter()) newStateData.putInt("opaqueCacheId", editText.id) - stateWrapper.updateState(newStateData) + // Committing the state synchronously re-measures and re-lays out the input (and anything + // depending on its size) before the current frame is drawn, so the view's size and its + // text never disagree for a frame (e.g. when a multiline input gains or loses a line). + stateWrapper.updateState(newStateData, StateWrapper.UpdateMode.unstable_Immediate) } // The event that contains the event counter and updates it must be sent first.