Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在react native中检查internet连接?_React Native - Fatal编程技术网

React native 如何在react native中检查internet连接?

React native 如何在react native中检查internet连接?,react-native,React Native,我想在启动应用程序时检查互联网连接,然后要求用户连接到互联网,如果他没有连接到互联网你应该使用“@react native community/netinfo”库。NetInfo曾经是react native的一部分,但后来它从核心中分离出来。如果您想观察网络状态的变化,只需使用提供的addEventListener方法 import NetInfo from "@react-native-community/netinfo"; NetInfo.fetch().then(s

我想在启动应用程序时检查互联网连接,然后要求用户连接到互联网,如果他没有连接到互联网

你应该使用“@react native community/netinfo”库。NetInfo曾经是react native的一部分,但后来它从核心中分离出来。如果您想观察网络状态的变化,只需使用提供的addEventListener方法

import NetInfo from "@react-native-community/netinfo";

NetInfo.fetch().then(state => {
    console.log("Connection type", state.type);
    console.log("Is connected?", state.isConnected);
});

const unsubscribe = NetInfo.addEventListener(state => {
    console.log("Connection type", state.type);
    console.log("Is connected?", state.isConnected);
});

// Unsubscribe
unsubscribe();

我相信这是一个复制品。检查这个问题:回答得好twboc
import NetInfo from "@react-native- 
community/netinfo";

NetInfo.fetch().then(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});

const unsubscribe = NetInfo.addEventListener(state => 
{
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});