【问题标题】:React Native Remove Autofill Space with State set InputReact Native Remove Autofill Space with State set Input
【发布时间】:2021-07-22 16:35:13
【问题描述】:

我在登录屏幕上遇到了问题,人们会自动填写他们的电子邮件,因此它会在屏幕末尾留下一个空格。我想澄清的是,当他们开始添加密码时,他们正在考虑做一个可能使用 .trim() 或 .replace() 的 onBlur 但我如何将它与我的 setEmail useState 一起使用?

屏幕打击:

const LoginScreen = ({ navigation }) => {

    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [error, setError] = useState('');

    const LogInUser = async() => {
        if(email && password) {
            try {
                await auth.signInWithEmailAndPassword(email, password)
                setEmail('')
                setPassword('')
                setError('')
                navigation.replace('Home')
                console.log('User Logged In = ');
            } catch (err){
                setError('Email or Password is wrong!');
                console.log(err);
            }
        }
    }

    return (
    <DismissKeyboard>     
        <View style={styles.screenStyle}>
        <View style={{ ...StyleSheet.absoluteFill }}>
                <Image source={require('../../assets/images/AccountBG.png')} style={{ flex: 1, height: null, width: null}} />
            </View>               
            <StatusBar hidden={true} />
            <View style={styles.loginBox}>
                <Text style={[styles.whiteText, styles.headTextH1, styles.headingBufferBottom]}>Welcome</Text>
                <Text style={styles.errorMessage}>{error}</Text>
                <TextInput
                    style={styles.inputStyles}
                    placeholder="Email"
                    placeholderTextColor="#000"
                    value={email}
                    onChangeText={setEmail}
                    onBlur={cleanEmail}
                />
                <TextInput
                    style={styles.inputStyles}
                    secureTextEntry
                    placeholder="Password"
                    placeholderTextColor="#000"
                    value={password}
                    onChangeText={setPassword}
                />
                <OGButton title="Login" onPress={()=> LogInUser()} />
            </View>
            <View style={[styles.registerBox, styles.whiteText]}>
                <Text style={styles.whiteText}>Don't have an account? | </Text>
                <Text style={[styles.underlineText, styles.whiteText]} onPress={() => navigation.navigate('Register')} >Register Here</Text>
            </View>
        </View>
    </DismissKeyboard>        
    );
}

【问题讨论】:

    标签: react-native trim use-state


    【解决方案1】:

    您可以修剪值和 setEmail 以及 onChangeText 中所需的任何检查

     onChangeText={(val) => {
      // have other checks if needed here
     setEmail(val.trim())
      }
     }
    

    【讨论】:

      猜你喜欢
      • 2021-03-09
      • 1970-01-01
      • 2018-10-08
      • 2020-12-13
      • 2020-03-20
      • 2018-11-14
      • 1970-01-01
      • 2018-07-28
      • 2020-05-04
      相关资源
      最近更新 更多