[英]Double trigger after pushing a button in React-native
我想應用兩個函數,在React-Native上按下一個簡單的按鈕后執行2個操作。 說實話,它應該很簡單但不知何故,第二個永遠不會被執行。
//Clean static function in its own class
class Aws {
static register(phonenumber, username, password) {
ClientConf = {
"UserPoolId" : "eee",
"ClientId" : "eee",
"Region": "us-east-1"
}
console.log("Aws Register");
AWSCognito.config.region ="ccc";
var poolData = {
UserPoolId : "bbb", // Your user pool id here
ClientId: "aaa"
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataPhoneNumber = {
Name : 'phone_number',
Value : phonenumber
}
var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributePhoneNumber);
userPool.signUp(username, password, attributeList, null, function(err, result){});
}
}
//Register Page, react-native Component
class RegisterPage extends Component {
renderScene(route, navigator) {
return (<View>
<Text style={styles.saved}>Create an account</Text>
<Form
ref='registrationForm'
onFocus={this.handleFormFocus.bind(this)}
onChange={this.handleFormChange.bind(this)}
label="Personal Information">
<InputField
ref='username'
placeholder='Username'
placeholderTextColor="#888888" />
<InputField
ref='phoneNumber'
placeholder='Phone Number'
placeholderTextColor="#888888" />
<InputField
ref='password'
placeholder='Password'
placeholderTextColor="#888888" />
</Form>
<TouchableHighlight>
onPress={Aws.register(this.state.formData.phoneNumber, this.state.formData.username, this.state.formData.password) && this.gotoNext.bind(this)}
underlayColor='#78ac05'>
<View><Text>Register</Text></View></TouchableHighlight>
</View>);
}
gotoNext() {
this.props.navigator.push({
id: 'MainPage',
name: 'mynamedpage',
});
}
}
我盡力簡化代碼。
這個問題出現了:
onPress={Aws.register(this.state.formData.phoneNumber, this.state.formData.username, this.state.formData.password) && this.gotoNext.bind(this)}
第一個功能很好執行但我的屏幕沒有移動到下一頁。 搬到時:
onPress={this.gotoNext.bind(this)}
頁面變化很好。 在這種情況下:
onPress={this.gotoNext.bind(this) && Aws.register(this.state.formData.phoneNumber, this.state.formData.username, this.state.formData.password)}
僅執行第二個功能。
如何設法執行這兩個操作? 我在之前的測試中沒有看到任何邏輯。 此外,我嘗試將它們都放在一個功能中,但問題仍然存在。 請注意,代碼已經按照目的進行了簡化(刪除了構造函數等等)並且編譯得很好。
嘗試在userPool.signUp()
回調函數中導航你可以將回調作為參數傳遞給你的Aws.register()
函數
//Clean static function in its own class
class Aws {
static register(phonenumber, username, password, callback) {
ClientConf = {
"UserPoolId" : "eee",
"ClientId" : "eee",
"Region": "us-east-1"
}
console.log("Aws Register");
AWSCognito.config.region ="ccc";
var poolData = {
UserPoolId : "bbb", // Your user pool id here
ClientId: "aaa"
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataPhoneNumber = {
Name : 'phone_number',
Value : phonenumber
}
var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributePhoneNumber);
userPool.signUp(username, password, attributeList, null, function(err, result){ callback() });
}
}
//Register Page, react-native Component
class RegisterPage extends Component {
renderScene(route, navigator) {
return (<View>
<Text style={styles.saved}>Create an account</Text>
<Form
ref='registrationForm'
onFocus={this.handleFormFocus.bind(this)}
onChange={this.handleFormChange.bind(this)}
label="Personal Information">
<InputField
ref='username'
placeholder='Username'
placeholderTextColor="#888888" />
<InputField
ref='phoneNumber'
placeholder='Phone Number'
placeholderTextColor="#888888" />
<InputField
ref='password'
placeholder='Password'
placeholderTextColor="#888888" />
</Form>
<TouchableHighlight>
onPress={Aws.register(this.state.formData.phoneNumber, this.state.formData.username, this.state.formData.password, this.gotoNext.bind(this))}
underlayColor='#78ac05'>
<View><Text>Register</Text></View></TouchableHighlight>
</View>);
}
gotoNext() {
this.props.navigator.push({
id: 'MainPage',
name: 'mynamedpage',
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.