我可以在上一个屏幕中调用函数-响应本机吗?

时间:2019-11-05 00:51:51

标签: javascript react-native react-navigation

调用后,我在 SignUp 屏幕中有一个函数“ signInWithPhoneNumber”,它将我导航到 Confirmation 屏幕,并且在导航后通过一些参数来确认我输入的验证码是收到, 因此,在“确认”屏幕中,我有一个“重新发送验证码”按钮,因此我的问题是 当我按“重新发送”时,我想在“注册”屏幕中调用“ signInWithPhoneNumber”功能以获取新代码

那么您的想法呢?有可能吗?

还是在确认屏幕中重写 signInWithPhoneNumber 并在按下重新发送按钮后调用它?

注册屏幕-功能

  signUp = async () => {

    const {phoneNumber} = this.state;
    this.setState({message: 'Sending code ...'});
    const phoneWithAreaCode = phoneNumber.replace(/^0+/, '+972');
    console.log(phoneWithAreaCode);
    auth()
      .signInWithPhoneNumber(phoneWithAreaCode, true)
      .then(confirmResult => {
        console.log('confirmResult', confirmResult);
        this.setState({confirmResult, message: 'Code has been sent!'});
        // this.createUserDatabase();
      })
      .then(() => {
        this.props.navigation.navigate('Confirmation', {
          message: this.state.message,
          confirmResult: this.state.confirmResult,
          createUser: uid => this.createUserDatabase(uid),
        });
      });
  };

确认屏幕-功能

confirmCode = codeInput => {

    const confirmResult = this.props.navigation.state.params.confirmResult

    if (confirmResult && codeInput.length) {
      confirmResult
        .confirm(codeInput)
        .then(user => {
          clearInterval(this.interval);
          const {params} = this.props.navigation.state;
          //Check if any users exist
          database()
            .ref(`users`)
            .limitToFirst(1)
            .once('value', snapshot => {
              if (snapshot.exists()) {
                console.log('exists!');
                return true;
              } else {
                params.createUser(user.uid);
                console.log('No user found Hah');
              }
            });
          this.setState({
            timer: 0,
            message: 'Code Confirmed!',
            isValid: true,
          });
        })
        .catch(error => {
          let errorCode = error.code;
          let errorMessage = error.message;
          console.log(errorCode);
          switch (errorCode) {
            case 'auth/invalid-verification-code':
              this.setState({message: 'Code is invalid', codeInput: ''});
              this.refs.codeInputRef2.clear();
              break;
            default:
              alert(`Please, Check your Messages!`);
              break;
          }
          console.log(errorMessage);
        });
    } else {
      console.log('Not here');
    }
  };

1 个答案:

答案 0 :(得分:0)

您可以将signInWithPhoneNumber发送到Confirmation屏幕,同时从SignUp屏幕导航到它,

this.props.navigation.navigate('Confirmation', {
          message: this.state.message,
          confirmResult: this.state.confirmResult,
          createUser: uid => this.createUserDatabase(uid),

          signInWithPhoneNumber: signInWithPhoneNumber // your actual function here
        });
      });

然后此功能将在Confirmation屏幕上作为道具提供,您可以在需要时调用它