导航问题 React native,stack navigator

Navigation problem React native ,stack navigator

我将数据推送到 api 的第一个代码,一切都很顺利,但我在导航到下一页时卡住了。

我有 phone 和密码文本输入。我的按钮点击代码是

<TouchableOpacity style={styles.buttonContainer} 
                    onPress={this.CheckTextInput 
                         }>
                    <Text style={styles.buttonText}>Login</Text>
                    </TouchableOpacity>

现在CheckTextInput方法我已经写好了HTTPPOST方法

CheckTextInput = () => {
//Handler for the Submit onPress
if (this.state.phone == '') {
    alert('Please Enter Phone/Email');
  //Check for the Name TextInput
} else if (this.state.password == '') {
    alert('Please Enter Password');
}else{

    var url = 'my_url';

    var data = {
            "phone":this.state.phone,
            "password": this.state.password
            }

            fetch(url, {
                method: "POST",
                headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json',
                        },
                body: JSON.stringify(data)
                })
                    .then(function(response){ 
                        return response.json();   
                    })
                    .then(function(data){ 
                        console.log(data)

                if(data.hasOwnProperty("success"))
                   {
                       let successmsg = data.success
                  // console.log('token is'+data.token)
                console.log('success is string'+ ''+ successmsg)

                       if(successmsg == true)
                          {

                       let token = data.token
                  // console.log('token is'+data.token)
                      console.log('token is string'+ ''+ token)

                    this.props.navigation.navigate( 'HomePage' ); //----> HERE THIS IS NOT NAVIGATING.

                          }else{
                              let message = data.message
                              alert(message);
                          }
                   }

                });

}



};

我遇到错误

Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'this.props.navigation')]

好吧,我已经解决了这个错误的所有解决方案,但没有解决任何问题。

有人请帮助我如何在条件下从一个页面导航到另一个页面。

fetch 您的数据和 then 语句中,尝试使用这样的箭头函数:

    fetch(url, {
          method: "POST",
          headers: {
               'Accept': 'application/json',
               'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
    })
    .then((response) => { 
          return response.json();   
    })
    .then((data) => { 
          console.log(data)
          .....
    })