简体   繁体   中英

How to call one function from another function in react native

This might be easy question but as i am new to react native it doesn't seems to be easy for me. I want to call one function(this. FunctionToOpenSecondActivity) from another function (this. funcToSumbitWithValidations), but it is not calling , no error nothing . Moreover when i am calling another Alert or something it is also calling However when i am calling it directly it is working perfectly. Here is my code:-

//ForgotPasswordActivity
class ForgotPasswordActivity extends Component {
  static navigationOptions =
    {
      title: 'ForgotPasswordActivity',
    };
  constructor(props) {
    super(props);
    this.state = { email: '' };

  }
  FunctionToOpenSecondActivity = () => {
this.props.navigation.navigate('otp');
  }
  testfunc() {
Alert.alert('testttinnn');
  }

  funcToSumbitWithValidations = (text) =>{
if(this.state.email==""){
  Alert.alert("field cannot be empty");
}else if(this.validate(this.state.email)){
  this.testfunc;
  this.FunctionToOpenSecondActivity;

}
  }
  validate = (text) => {
let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (reg.test(text) === false) {
  Alert.alert("Email is Not Correct");
  this.setState({ email: text })
  return false;
}
else {
  this.setState({ email: text })
   Alert.alert("Email is Correct");
  return true;
}
  }
  render() {
return (
  <View style={styles.container}>
    <Image
      style={styles.stretch}
      source={require('./img/login-screen.jpeg')}
    />
    <Text style={styles.txtstyle}>
      Retrieve Your OTP
        </Text>
    <TextInput
      style={styles.edittext}
      placeholder={'Enter email'}
      placeholderTextColor={'white'}
      inlineImageLeft='icons_gmail'
      inlineImagePadding={20}
      onChangeText={(email) => this.setState({ email })}
    />
    <TouchableOpacity onPress={this.funcToSumbitWithValidations}>
      <Image
        style={styles.btnLg}
        source={require('./img/getotp.jpg')}
      />
    </TouchableOpacity>


  </View>
);
  }
}

First you bind FunctionToOpenSecondActivity in constructor to access scope of this as you are accessing " this " in your function. like :

   this.FunctionToOpenSecondActivity = this.FunctionToOpenSecondActivity.bind(this);

and then call Like this:- this.FunctionToOpenSecondActivity();

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