简体   繁体   中英

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 contains username and password textinput. BottomButton contains the submit button

I want to navigate to new activity when submit button clicked. navigation to new screen is working perfectly but before navigating i want to null check the TextInput which are on

I am new to React-Native. Complete Beginner here. I want even know what to do. Help.

ImgBack.js file

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

class bottomButon extends Component {

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

} }

export default bottomButon; ```

solution:

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);
    }
  };

it helped me solve my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM