简体   繁体   中英

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

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

Screenshot:

在此处输入图像描述

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?

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

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:

在此处输入图像描述

<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}
    />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM