我想应用两个函数,在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)}
仅执行第二个功能。
如何设法执行这两项操作?我在之前的测试中没有看到任何逻辑。 此外,我尝试将它们都放在一个功能中,但问题仍然存在。 请注意,代码已经按照目的进行了简化(删除了构造函数等等)并且编译得很好。
答案 0 :(得分:0)
尝试导航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',
});
}
}