简体   繁体   English

在React Native中如何从另一个函数调用一个函数

[英]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 . 我想从另一个函数(this.funcToSumbitWithValidations)调用一个函数(this.FunctionToOpenSecondActivity),但它没有调用,没有错误,没有什么。 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. 首先,当您在函数中访问“ this ”时,在构造FunctionToOpenSecondActivity中绑定FunctionToOpenSecondActivity以访问此函数的范围。 like : 喜欢 :

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

and then call Like this:- this.FunctionToOpenSecondActivity(); 然后像这样调用: this.FunctionToOpenSecondActivity();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从另一个组件调用 function 以响应本机? - How would I call a function from another component in react native? 如何在React Native中使用来自另一个类的参数调用函数? - How to call a function with parametrs from another class in react native? React Native:如何从另一个组件调用 function - React Native: How to call a function from another component 如何将 function 从一个组件调用到另一个组件? 反应 - How can I call a function from one component to another? React 如何在REACT中从另一个类中调用一个类中的函数 - How to Call a Function in One Class From Another Class in REACT 如何在本机反应中调用另一个 function 内部的 function - how to call a function that is inside another function in react native 在React Native中如何从导航栏中调用函数? - How to call a function from navigationbar in React native? 如何从全局 function 中调用 function 的 function - how to call function of function in react native from the global function 如何在反应原生的单个按钮上一个接一个地调用多个 function - how to call multiple function one by one on single button in react native 如何在 React Native 中从另一个功能组件更改一个功能组件的状态? - How to change state of one function component from another function component in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM