简体   繁体   中英

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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