簡體   English   中英

當文本輸入和提交按鈕都在 React Native 中的不同 js 文件中時,如何在單擊提交時進行空檢查並導航到新屏幕?

[英]How to null check and navigate to new screen on click submit when textinput and submit button both are in different js files in react native?

render(){
  return (
    <ImageBackground
      source={require('../images/back02.png')}
      style={styles.bgscreen}
    >
    <KeyboardAvoidingView behavior='position'>
      <Image
        style={styles.headImage}
        source={require('../images/login_img.png')}
      />
      <ImgBack/>
      </KeyboardAvoidingView>
      <BottomButton navigation={this.props.navigation} />
    </ImageBackground>
  );}
}

ImgBack包含用戶名和密碼文本輸入。 BottomButton包含提交按鈕

單擊提交按鈕時,我想導航到新活動。 導航到新屏幕工作正常,但在導航之前,我想空檢查打開的 TextInput

我是 React-Native 的新手。 在這里完成初學者。 我什至想知道該怎么做。 幫助。

ImgBack.js 文件

class imgBack extends React.Component  { 

    constructor()
    {
      super();
      this.state = { hidePassword: true }
    }

    managePasswordVisibility = () =>
    {
      this.setState({ hidePassword: !this.state.hidePassword });
    }
    usernameValidate = (EnteredValue) =>{

        var TextLength = EnteredValue.length.toString();
        if(TextLength == 10 ){
          Alert.alert("Sorry, You have reached the maximum input limit.")
        }
        else if(TextLength == 0){
          Alert.alert("Username can't be blank")
        }
      }

      passValidate = (EnteredValue) =>{

        var TextLength = EnteredValue.length.toString();
        if(TextLength == 10 ){
          Alert.alert("Sorry, You have reached the maximum input limit.")
        }
        else if(TextLength == 0){
          Alert.alert("Username can't be blank")
        }
      }

    render(){
        return (
        <ImageBackground resizeMode='contain'
            source={require('../images/login_back.png')}
            style={{
                marginHorizontal: 10,
                height: 290,
                padding: 30,
            }}>

            <View style={
                styles.textInputContainer
            } >
                <TextInput
                    placeholder="Username"
                    underlineColorAndroid = "transparent"
                    placeholderTextColor="#000000"
                    maxLength={10}
                    onChangeText={ EnteredValue => this.usernameValidate(EnteredValue) }
                />
            </View>

            <View style = { styles.textInputContainer }>
                <TextInput
                onChangeText={ EnteredValue => this.passValidate(EnteredValue) }
                underlineColorAndroid = "transparent"
                secureTextEntry = { this.state.hidePassword }
                placeholder="Password"
                placeholderTextColor="#000"
                />
                    <TouchableOpacity style = { styles.visibilityBtn } onPress = { this.managePasswordVisibility }>
                        <Image source = { ( this.state.hidePassword ) ? require('../images/eye_close_icon.imageset/eye_close_icon.png') : require('../images/eye_icon.imageset/eye_icon.png') } style = { styles.btnImage } />
                    </TouchableOpacity>
            </View>
        </ImageBackground>
    )
}
} ```

**Bottombutton File**

類底部按鈕擴展組件{

render(){
    return (
    <ImageBackground
        style={{ height: 80, marginLeft: '20%', marginTop: 10 }}
        resizeMode={'center'}
        source={require('../images/btn_back.png')} >
        <TouchableOpacity
        onPress={ this.login }  >
            <Text style={{ textAlign: 'center', marginTop: 25 }}>Submit & SYNC</Text>
        </TouchableOpacity>
    </ImageBackground>
)

} }

導出默認底部按鈕; ``

解決方案:

constructor(props) {
    super(props);
    this.state = {
      hidePassword: true,
      username: '',
      password: '',
    };
  }

  validUserPass = async () => {
    try {
      if (this.state.username === '') {
        Alert.alert('Username is required !');
      } else if (this.state.password === '') {
        Alert.alert('Password is required !');
      } else {
        this.props.navigation.navigate('selectEmoji');
      }
    } catch (error) {
      console.warn(error);
    }
  };

它幫助我解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM