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