'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.
Solution 1:[1]
The keyboard should open automatically when a <TextField /> is focused. You can use the autoFocus prop to make it focus when the element mounts (link)
Solution 2:[2]
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} />
}
Solution 3:[3]
This trick worked for me
setTimeout(() => InputRef.current.focus(), 100)
Solution 4:[4]
another solution :)
import React, { useRef } from 'react'
export function mycomponent(props) {
const inputRef = useRef();
return(
<TextInput
ref={inputRef}
onLayout={()=> inputRef.current.focus()}>
</TextInput>
)
}
Solution 5:[5]
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}
/>
Solution 6:[6]
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 });
}
}
Solution 7:[7]
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}
/>
- I needed the constructor to register the reference
- The handler sets a local variable to true
- autoFocus will trigger the field to be focused. The keyboard sometimes opens up on android simulator (this I cannot explain).
- ref is for the reference to the DOM element
Solution 8:[8]
You can maintain a ref to the TextInput as textInputRef and use this.textInputRef.focus()
Solution 9:[9]
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({});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ziarno |
| Solution 2 | bobu |
| Solution 3 | hamzaahzam |
| Solution 4 | amirhosein |
| Solution 5 | user2573099 |
| Solution 6 | |
| Solution 7 | |
| Solution 8 | naheed.shamim |
| Solution 9 |
