From 51f49ca9982f24de08f5a5654a5210e547bb5b86 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Wed, 13 Jul 2022 16:41:17 -0700 Subject: [PATCH] Fix TextInput dropping text when used as uncontrolled component with `defaultValue` Summary: A layout-impacting style change will trigger a layout effect hook within `TextInput`. This hook fires a ViewManager command to set the text input based on the known JS value: https://github.com/facebook/react-native/blob/d82cd3cbce1597512bb2868fde49b5b3850892a0/Libraries/Components/TextInput/TextInput.js#L1009 The JS value is determined using `value` if set, falling back to `defaultValue`. If a component uses `TextInput` as an uncontrolled component, and does not set this value, the command wipes text input back to the default value. This does not happen on re-render of the JS side, despite setting text prop, since the underlying native property never changes/triggers a rerender. This change alters the logic to prefer `lastNativeText` instead of `defaultValue` when available, to retain the updated `TextInput` content on relayout. Reviewed By: javache Differential Revision: D37801394 fbshipit-source-id: d56c719d56bebac64553c731ce9fca8efc7feae9 --- Libraries/Components/TextInput/TextInput.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 273d64914dd7ec..4dc547d01c5698 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -976,6 +976,8 @@ function InternalTextInput(props: Props): React.Node { const text = typeof props.value === 'string' ? props.value + : typeof lastNativeText === 'string' + ? lastNativeText : typeof props.defaultValue === 'string' ? props.defaultValue : '';