简体   繁体   English

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

[英]Double trigger after pushing a button in React-native

I would like to apply two functions, do 2 actions after pushing a simple button on React-Native. 我想应用两个函数,在React-Native上按下一个简单的按钮后执行2个操作。 To be honest, it should be very simple but somehow, the second one is never executed. 说实话,它应该很简单但不知何故,第二个永远不会被执行。

//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',
    });
  }
}

I tried to simplify the code to the best I could. 我尽力简化代码。

The problem is occuring at this line: 这个问题出现了:

  onPress={Aws.register(this.state.formData.phoneNumber, this.state.formData.username, this.state.formData.password) && this.gotoNext.bind(this)}

The first function is well-executed but my screen does not move to the next page. 第一个功能很好执行但我的屏幕没有移动到下一页。 When moving to: 搬到时:

  onPress={this.gotoNext.bind(this)}

The page is changing well. 页面变化很好。 And in this case: 在这种情况下:

  onPress={this.gotoNext.bind(this) && Aws.register(this.state.formData.phoneNumber, this.state.formData.username, this.state.formData.password)} 

Only the second function is executed. 仅执行第二个功能。

How can I manage to execute those two actions? 如何设法执行这两个操作? I do not see any logics in the previous tests. 我在之前的测试中没有看到任何逻辑。 Also, I tried putting them both in a single function but the problem remains. 此外,我尝试将它们都放在一个功能中,但问题仍然存在。 Note that the code has been simplify at its best on purpose (removed constructor etc...) and is compiling well. 请注意,代码已经按照目的进行了简化(删除了构造函数等等)并且编译得很好。

Try navigating away in callback function of userPool.signUp() You can pass callback as parameter to your Aws.register() function 尝试在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