Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In React Native, how can I avoid having a user click a submit button twice, when they are writing in a TextInput?

Tags:

react-native

I have a very simple form for writing comments, with a <TextInput> and an <IconButton>:

Screenshot:

enter image description here

Code:

  <View style={styles.container}>
    <TextInput
      mode={'outlined'}
      onChangeText={(val) => setCommentText(val)}
      value={commentText}
      label={'Write a comment...'}
    />
    <IconButton
      icon="send"
      onPress={addComment}
    />
  </View>

The problem is that users have to click the Send button twice. The first click just blurs the input and dismisses the keyboard, but the button doesn't register a click. Then when the TextInput is blurred they can actually click the Submit button.

How can I make it possible to click the Send button only once, when the TextInput has focus?

like image 952
Weblurk Avatar asked Oct 23 '25 15:10

Weblurk


2 Answers

Changing onPress to onTouchStart in the <Button> solved it. No need for tapping the button twice anymore.

like image 130
Weblurk Avatar answered Oct 25 '25 12:10

Weblurk


Actually this behaviour is normal . if the keyboard is open and you want to interact with screen , first click will dismiss the keyboard and second one will work . so as the best practice you can have a button within your keyboard which clicking on that will trigger the call request you want :

enter image description here

<TextInput
      mode={'outlined'}
      onChangeText={(val) => setCommentText(val)}
      value={commentText}
      returnKeyType="send"
      onSubmitEditing={() => {
        // call you api or whatever function here;
      }}
      label={'Write a comment...'}
      blurOnSubmit={false}
    />
like image 31
Amir Doreh Avatar answered Oct 25 '25 12:10

Amir Doreh