Reputation: 15
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>
);
}
}
Upvotes: 0
Views: 3497
Reputation: 670
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();
Upvotes: 1