Clear Text on single tap Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Clear Text on single tap without wasting too much if your time.

The question is published on by Tutorial Guruji team.

When I click on the cross icon first time, it lowers the keyboard and on the second tap, it clears the text in the search bar. I wanted to clear the text on the first tap I have tried other solutions as well but nothing worked. How can i clear the text on the first click?

    return (
      <View style={styles.FlatList_header}>
        <View style={styles.header_style}>
          <Input
            autoFocus={true}
            style={styles.textInputStyle}
            onChangeText={text => this.search(text)}
            value={this.state.text}
          />
          <Icon
            name="close"
            style={styles.crossIcon}
            onPress={() => {
              this.search('');
            }}
          />
        </View>
      </View>
    );
  };

  render() {
    const {data, onPress} = this.props;
    return (
      <>
        <SafeAreaView
          style={{flex: 1, backgroundColor: customColor.defaultGreen}}>
          <View style={styles.MainContainer}>
            <FlatList
              data={data}
              renderItem={({item}) => (
                <View
                  style={{
                    flex: 1,
                    borderWidth: 0.5,
                    borderColor: customColor.textLightGrey,
                  }}>
                  <Text
                    style={styles.FlatList_Item}
                    onPress={() => onPress(item)}>
                    {item?.taskName}
                  </Text>
                </View>
              )}
              enableEmptySections={true}
              ListHeaderComponent={this.Render_FlatList_Sticky_header}
              keyExtractor={item => item.id}
            />
          </View>
        </SafeAreaView>
      </>
    );
  }
}

Answer

In your onPress function do this.

     onPress={() => {
     Keyboard.dismiss()
              this.search('');
     this.setState(text: '')
           }}

Also, you are not setting your state while onChangeText, Do this in your onChangeText

onChangeText={(text)=>this.setState({text})}

Edit: Add this into your FlatList

 keyboardShouldPersistTaps='always'
We are here to answer your question about Clear Text on single tap - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji