[英]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.