如何在使用 expo netInfo 和托管工作流的 React Native 应用程序中显示无 Internet 连接

问题描述

我是本机反应的新手。我创建了一个名为 NoConnection.js 的屏幕 现在我想在用户手机上没有互联网连接时显示名为“没有互联网连接,请将您的手机连接到互联网”的消息。当用户连接到互联网时,它导航到我的应用程序的第一个屏幕。我希望通过带有托管工作流程的 expo netInfo 来实现这一点。 我希望自动发送该消息。我不希望用户单击按钮然后只显示消息 这是我的代码

import React,{ Component } from "react";
import { View,Text,Button,Alert,Platform,alert } from "react-native";
import NetInfo from "@react-native-community/netinfo";
import navigation from "../navigation";

export default class NoConnection extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  CheckConnectivity = () => {
    const unsubscribe = NetInfo.addEventListener(state => {
      console.log('Connection type',state.type);
      console.log('Is connected?',state.isConnected);
    });
    unsubscribe()
  };
  
  render() {
    return (
      <View>
        <Button
          onPress={() => this.CheckConnectivity()}
          title="Check Internet Connectivity"
          color="#841584"
          accessibilityLabel="Learn more about this purple button"
        />
      </View>
    );
  }
}

netinfo

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)