繁体   English   中英

在React-native中按下按钮后双击

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM