首页 > 解决方案 > 当互联网连接不好时,在本机反应中显示错误

问题描述

我正在对我的 react native 应用程序进行最后修改。

好吧,我得到的问题是,当我输入我的信息进行连接时,我会在一个小框对话框中显示您的数据验证正在运行(这次是用于比较用户输入的信息和 sql server 数据)所以当它正确时,我正在访问主页。

我的问题是有时,互联网不是那么好,所以我需要当验证需要更长的时间来显示错误说“请验证您的互联网连接”

不知道有没有方法或者具体功能!!

 constructor(props){
       super(props);
       this.state={
         isModalVisible:false,
}}
 openModal = () =>{
    this.setState({
    isModalVisible:true
    }) }


    userRegister = () =>{
      this.openModal();

   fetch('http://192.168.1.7:80/recup.php',{
   method:'post',
   header:{
   'Accept':'application/json',
   'Content-type' :'application/json'
   },
   body:JSON.stringify({email:usermail,password:password,})})
   .then((responseData)=>responseData.text())
   .then((responseJson)=>{
   if(responseJson =='Data Matched'){
     this.props.navigation.navigate( 'Welcome')}
   else{
     alert("Merci de saisir vos données correctement")}
   })
   .catch((error)=>{console.error(error);})
   }
  
    render() {
if(!this.state.isLoading){
  return(
       ....
        <TouchableOpacity style={styles.butt} onPress={this.userRegister}> 
                           <Text>Connection</Text</TouchableOpacity>
      ....

标签: react-native

解决方案


如果你想访问这个(我的意思是如果你想获取)url http://192.168.1.7:80/recup.php你应该有正确的互联网连接。您可以使用这个 react-native-netinfo 库来处理这个问题。https://github.com/react-native-community/react-native-netinfo


推荐阅读