0

I need quick help, please. When I put an invalid email and hit enter it shows me in the alert prompt that email is invalid but Whenever I am typing in my text box to correct my email the alert prompt is showing every time. I need to get rid of these. please, somebody, help me.

<View style={styles.inputContainer}>
            <TextInput
              autoCapitalize="none"
              autoCompleteType="off"
              autoCorrect={false}
              value={username}
              onChangeText={(text) => this.setState({username: text})}
              placeholder="Email"
              underlineColorAndroid="transparent"
              style={styles.textInput}></TextInput>
          </View>

The error alert prompt code

 <View>{this.state.error ? Alert.alert('Email invalid') : null}</View>
3
  • If you have the Alert in the render method it's being called in every render unless this.state.error is false. Attempt to only call the alert when the error is being set or clear the error in state. Apr 10, 2020 at 20:57
  • Ahh man..Can u pls pls give me little code example..plss Apr 10, 2020 at 21:01
  • onChangeText is call every time you are typing, thus your Alert in the view is called each time.
    – CR7
    Apr 10, 2020 at 21:30

1 Answer 1

0

While you got the error this.state.error remain true and when you enter text into textInput on each render this error executes again, Solution for this is either remove this error state from render or set state false after showing error.

 <View>{this.state.error ? Alert.alert('Email invalid') : null}</View>

Remove above line from render or make sure this line not execute after displaying error on the fisrst attempt by putting your error state to false

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.