'How to open keyboard on button press in react native?

I have a button and when it is pressed I would like it to open the keyboard and focus on a text input component.

For a minimal code example what I am trying to do is this-

      <View>
          <AddSomething
            textChange={textInput => this.setState({ textInput })}
            addNewItem={this.addItem.bind(this)}
            textInput={this.state.textInput}
            ref={not sure what goes here} //passing these as props to a text input
          />
          <FloatingButton tapToAddHandler={this.onFloatingButtonPress.bind(this)} />
      </View>

then some helper function where I handle the button press (this.onFloatingButtonPress)



Solution 1:[1]

Proposal a react-hook version :

const inputRef = React.useRef()

return (
  <TextInput ref={inputRef} />
  <TouchableOpacity onPress={() => inputRef.current.focus()}>
    <Text>Press</Text>
  </TouchableOpacity>
)

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