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 automati
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 });
}
}
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} />
}
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}
/>
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)