每次我在 React Native 中编辑我的文本输入字段时都会显示警报提示

Alert prompt is showing every time i edit my text input field in react native

我需要快速帮助,谢谢。当我输入无效的电子邮件并按回车键时,它会在警报提示中显示电子邮件无效,但每当我在文本框中键入内容以更正我的电子邮件时,每次都会显示警报提示。我需要摆脱这些。请有人帮助我。

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

错误提示代码

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

当您收到错误 this.state.error 时仍然 true 并且当您在每次渲染时将文本输入 textInput 时,此错误会再次执行,对此的解决方案是从中删除此错误状态显示错误后渲染或设置状态为 false。

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

通过将 error state 设置为 false

,从渲染中删除上面的行,或确保在第一次尝试显示错误后不执行此行