Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open keyboard automatically in React Native?

Tags:

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.

like image 983
Harkirat Saluja Avatar asked Feb 25 '17 12:02

Harkirat Saluja


People also ask

Why we use KeyboardAvoidingView in React Native?

It is a component to solve the common problem of views that need to move out of the way of the virtual keyboard. It can automatically adjust either its height, position, or bottom padding based on the keyboard height.


2 Answers

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)

like image 101
Ziarno Avatar answered Sep 23 '22 14:09

Ziarno


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} /> } 
like image 25
bobu Avatar answered Sep 24 '22 14:09

bobu