0

我想 setInterval 连接检查,但它不适合我。有人可以向我解释为什么以及我该怎么做吗?谢谢!

import React, { Component } from 'react';
import { StyleSheet, TouchableWithoutFeedback, Keyboard, Text, View, TouchableOpacity, Image, Alert, TextInput, FlatList, Button, RefreshControl } from 'react-native';
import NetInfo from "@react-native-community/netinfo";

import * as signalR from '@aspnet/signalr';

const hubUrl = '/chatHub';
const hub = new signalR.HubConnectionBuilder()
    .withUrl(hubUrl)
    .configureLogging(signalR.LogLevel.Information)
    .build();

setInterval(NetInfo.getConnectionInfo().then((connectionInfo) => {
    alert(
       'Initial, type: ' +
        connectionInfo.type +
        ', effectiveType: ' +
       connectionInfo.effectiveType,
     );
  }),1000);

export default class ChatScreen extends Component { }
4

1 回答 1

0

调用 setIntervalcomponentDidMount()

为什么不使用 NetInfo 事件侦听器?有什么特殊原因吗?

NetInfo.addEventListener(state => {
  console.log("Connection type", state.type);
  console.log("Is connected?", state.isConnected);
});
于 2019-09-14T07:01:54.213 回答