react-native-netinfo: The NetInfo.addEventListener not triggered when network state changed(From on to off vice versa)

ISSUE : The NetInfo.addEventListener not triggered when network state changed(From on to off vice versa). Eg:first time my code registering for this event and getting network status.So i am expecting that it should trigger whenever network state changes. My dependency versions: “@react-native-community/netinfo”: “^5.2.0”, “react”: “16.9.0”, “react-native”: “0.61.5” I have used NetInfo in my code like this(i am mentioning issue related code only) : user-service.js

import NetInfo from "@react-native-community/netinfo";
export const getNetWorkStatus = NetInfo.addEventListener(state => {
     console.log("Connection type", state.type);
    console.log("Is connected?", state.isConnected);
});

DashBoard.js

[import React, { Component } from 'react';
import * as UserService from '../service/user-service'

export default class DashBoard extends Component {
    componentDidMount(){
        UserService.getNetWorkStatus()

    }
}

I am calling for service method to registering for that event in one my component’s componentDidMount.But whenever network state changes, that event not listening or triggering. Did i miss any configuration ?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

👋 Take a look at the example here for how to use the event subscription correctly.

Most likely yes, however there is ongoing discussion about where it is truly working on RN-current, with tests on real devices with SIM cards active appearing to work fine, but other tests failing sometimes. And no one is stepping up to conclusively test things, everyone is just dropping by with a comment here or a request for help there then disappearing.

If you’d like to propose a docs PR for v7+ of this module being compatible with rn65+ I think that’s a statement reasonably supported by available evidence though. The event listener changes in RN65 forced some changes here which are seemingly not backwards compatible.

@vlxdisluv please make sure you are on current stable versions of netinfo and react-native (0.66.xx)

Having the same issue, have tried the useNetInfo hook, the NetInfo.addEventListener and also a setTimeout with NetInfo.fetch(), but all sends back the state from when the app was first launched. If I then close the app and open it again, it correctly identifies the new state until I again change it.