From 0569d4c4315d61d2d8f4ab628a54eb1e1db45dc2 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Mon, 2 Mar 2020 11:31:10 -0800 Subject: [PATCH] Remove JS autoFocus implementation (#27923) Summary: Follow up to https://github.com/facebook/react-native/issues/27803 and https://github.com/facebook/react-native/issues/27924. We no longer need to call focus on mount from JS as both iOS and Android implements it natively now. ## Changelog [General] [Fixed] - Remove JS autoFocus implementation Pull Request resolved: https://github.com/facebook/react-native/pull/27923 Test Plan: Test that focus works in RN Tester with this, https://github.com/facebook/react-native/issues/27803 and https://github.com/facebook/react-native/issues/27924 Differential Revision: D19956373 Pulled By: TheSavior fbshipit-source-id: 5d99ead55011663b3edaf499ac7616765a24cb50 --- Libraries/Components/TextInput/TextInput.js | 38 --------------------- 1 file changed, 38 deletions(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 334fc8246a0747..df98a6402e51e0 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -699,42 +699,6 @@ type ImperativeMethods = $ReadOnly<{| const emptyFunctionThatReturnsTrue = () => true; -function useFocusOnMount( - initialAutoFocus: ?boolean, - inputRef: {| - current: null | React.ElementRef>, - |}, -) { - const initialAutoFocusValue = useRef(initialAutoFocus); - - useEffect(() => { - // We only want to autofocus on initial mount. - // Since initialAutoFocusValue and inputRef will never change - // this should match the expected behavior - if (initialAutoFocusValue.current) { - const focus = () => { - if (inputRef.current != null) { - inputRef.current.focus(); - } - }; - - let rafId; - if (Platform.OS === 'android') { - // On Android this needs to be executed in a rAF callback - // otherwise the keyboard opens then closes immediately. - rafId = requestAnimationFrame(focus); - } else { - focus(); - } - - return () => { - if (rafId != null) { - cancelAnimationFrame(rafId); - } - }; - } - }, [initialAutoFocusValue, inputRef]); -} /** * A foundational component for inputting text into the app via a * keyboard. Props provide configurability for several features, such as @@ -935,8 +899,6 @@ function InternalTextInput(props: Props): React.Node { text, ]); - useFocusOnMount(props.autoFocus, inputRef); - useEffect(() => { const inputRefValue = inputRef.current;