From 0655837c7387ffd41ddea6cfe560010b33196f29 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Mon, 30 Nov 2026 14:28:06 +0100 Subject: [PATCH] [Android] Commit TextInput native state updates synchronously A multiline TextInput re-flows and repaints its text on the keystroke, but its frame only changed once the state update sent from the text watcher was flushed asynchronously (next Choreographer tick, commit and layout on the JS thread, mount on the following UI frame). Every change that adds or removes a line therefore drew one frame with the new text and the old height, which showed up as a content jump. Dispatch that state update with UpdateMode.unstable_Immediate so the commit, layout and mount happen inside onTextChanged, before the frame is drawn, and refresh the cached spannable before notifying the text watchers so the synchronous measure sees the new text. Fixes #46813 Co-Authored-By: Claude Fable 5.1 --- .../com/facebook/react/views/textinput/ReactEditText.kt | 6 ++++-- .../react/views/textinput/ReactTextInputTextWatcher.kt | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) 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.