react native keyboard does not pop up when the Android Textinput autoFocus is true

Viewed 280

When entering the screen the keyboard should open automatically, the problem only happens on Android.

Is there another solution to open the keyboard automatically?

react-native: 0.64.2

1 Answers

You can use a useRef hook to do this. Live example (https://snack.expo.dev/@heytony01/smiling-candy).

export default function App() {
  // Use to get reference to keyboard
  const textInputRef = React.useRef();

  // Focuses on keyboard once screen starts 
  React.useEffect(()=>{
    // Use the textInput focus function
    textInputRef.current.focus(); // opens keyboard
  },[])

  return (
    <View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
        <TextInput ref={textInputRef} placeholder="Type here" style={{backgroundColor:"lightgray"}}/>
    </View>
  );
}
Related