Harkirat Saluja
Harkirat Saluja

Reputation: 8124

How to open keyboard automatically in React Native?

I have a screen in my React Native application in which I have few text fields.

I was wondering if there is any way in which on screen load my keyword opens automatically and focuses on my first text input field?

I was searching for something like stateAlwaysVisible in android.

Upvotes: 41

Views: 93383

Answers (11)

user29670484
user29670484

Reputation: 1

just use autoFocus prop of TextInput. It worked even in a modal :

<TextInput
    placeholder={placeholder || "Enter text"}
    value={text}
    onChangeText={setText}
    editable={!readOnly}
    autoFocus={true}
/>

Upvotes: 0

amirhosein
amirhosein

Reputation: 921

another solution :)

import React, { useRef } from 'react'

export function mycomponent(props) {
   const inputRef = useRef();
   
   return(
         <TextInput 
           ref={inputRef}
           onLayout={()=> inputRef.current.focus()}>
         </TextInput>
   )
}

Upvotes: 13

Daniel Reina
Daniel Reina

Reputation: 6407

None of the solutions above worked for me when using a Modal. However, a combination of them did work.

Using the Modal's onShow and a setTimeout did the job for me.

<Modal
  onShow={() => {
    setTimeout(() => {
      inputRef?.current?.focus()
    }, 50)
  }}
>
  {/* ... */}
  <TextInput
    ref={inputRef}
  />
</Modal>

Upvotes: 2

Ziarno
Ziarno

Reputation: 7572

The keyboard should open automatically when <TextInput /> is focused. You can use the autoFocus prop to make it focus when the element mounts (link)

Upvotes: 55

questerstudios
questerstudios

Reputation: 189

This is how it looks in code, using the setTimeout() Hook

import { StyleSheet, Text, View, TextInput } from "react-native";
import React from "react";

const HomeScreen = () => {
  const inputRef = React.useRef();

  setTimeout(() => inputRef.current.focus(), 100);
  return (
    <View style={{ paddingVertical: 20 }}>
      <Text>circle</Text>
      <TextInput ref={inputRef} />
    </View>
  );
};

export default HomeScreen;

const styles = StyleSheet.create({});

Upvotes: 0

user2573099
user2573099

Reputation: 99

It worked for me.

<TextInput
        autoFocus={false}
        autoCapitalize="none"
        onChangeText={(val) => {
            props.onChange(val);
        }}
        value={null}
        defaultValue={props.defaultValue}
        ref={(ref) => {

            if( ref !== undefined && ref && !ref.isFocused() ){

                ref.focus();
            }
        }}
        {...propsMerge}
    />

Upvotes: 1

hamzaahzam
hamzaahzam

Reputation: 450

This trick worked for me

setTimeout(() => InputRef.current.focus(), 100)

Upvotes: 28

yet.another.coder
yet.another.coder

Reputation: 390

You can maintain a ref to the TextInput as textInputRef and use this.textInputRef.focus()

Upvotes: 0

Cyrois
Cyrois

Reputation: 479

This seems to be working in my simulator. Have not confirmed on a real device.

constructor(props) {
  super(props);
  this.buildNameTextInput = React.createRef();
}
<TouchableOpacity
  style={styles.mainButton}
  onPress={() => {
    this.buildNameTextInput = true
    this.props.setSaveBuildModal(true)
  }}
>
  <Text style={styles.mainButtonText}> Save Build </Text>
</TouchableOpacity>

<TextInput
  autoFocus={!!this.buildNameTextInput}
  ref={this.buildNameTextInput}
  placeholder="Type a Name"
  autoCapitalize="words"
  style={{...styles.heroBtnText, marginVertical: 50}}
  onChangeText={buildName => this.props.setCurrentBuildName(buildName)}
  value={this.props.buildName}
/>
  1. I needed the constructor to register the reference
  2. The handler sets a local variable to true
  3. autoFocus will trigger the field to be focused. The keyboard sometimes opens up on android simulator (this I cannot explain).
  4. ref is for the reference to the DOM element

Upvotes: 0

bobu
bobu

Reputation: 945

My way (there was a problem with focusing and showing a keyboard on component render)

import { InteractionManager, TextInput } from 'react-native';

...

inputRef = React.createRef();

componentDidMount() {
  this.focusInputWithKeyboard()
}

focusInputWithKeyboard() {
  InteractionManager.runAfterInteractions(() => {
    this.inputRef.current.focus()
  });
}

render() {
  <TextInput ref={this.inputRef} />
}

Upvotes: 16

Mazel
Mazel

Reputation: 49

The selected solution did not work for me due to stack navigation (see comment of "SoundStage" on selected solution)

I added a new variable openTheKeyboard to the state initially set to false.

My hacky solution:

componentDidMount() {
  this.setState({ openTheKeyboard: true });
}

componentDidUpdate() {
  if (this.state.openTheKeyboard) {
    this.textInput.focus();
    this.setState({ openTheKeyboard: false });
  }
}

Upvotes: -1

Related Questions