-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NetInfo.isConnected.addEventListener only works in root componentDidMount #18130
Comments
Any Update? |
Didn't find a specific fix for the issue, however our team found a workaround for it. We are using redux-saga in order to manage the connectivity state of the app and using an event channel to monitor it:
Not sure why this works and setting it up in a |
My workaround is to use import {NetInfo, Platform} from 'react-native';
class NetworkStatusProvider extends React.Component {
componentDidMount() {
// Android hack for NetInfo addEventListener
// not being detected on initial mount
if (Platform.OS === 'android') {
NetInfo.isConnected.fetch().then(isConnected => {
console.log('Expected to see value logged: ', isConnected);
})
}
NetInfo.isConnected.addEventListener(
'connectionChange',
(value) => {
console.log('Expected to see value logged: ', value);
}
);
}
render() {
return this.props.children;
}
} |
In my experience the |
@ekimlinger Could you retest this issue in newest version of React Native (that would be v0.57.1 as of now)? I'm using React Native v0.57.0 and it works fine on physical Android device (OnePlus6, Android v8.1.0). It detects offline/online changes for I also tested this on iOS Simulator (iPhone 6, iOS 12) on macOS Mojave. The iOS simulator correctly interprets my Mac's Wifi switching off, but doesn't pick up an event when WiFi is switched back on. My component structure:
This is how I set up my subscribes/unsubscribes inside _handleFirstConnectivityChange = isConnected => {
isConnected ? this.props.internetConnected() : this.props.internetDisconnected()
}
componentDidMount () {
NetInfo.isConnected.addEventListener(
'connectionChange',
this._handleFirstConnectivityChange
)
}
componentWillUnmount () {
NetInfo.isConnected.removeEventListener(
'connectionChange',
this._handleFirstConnectivityChange
)
} |
Still not working for me on react-native 0.57.3 My component structure:
` |
@gmaggio |
I am using as the example. When it change at the first time it working grate but when connectivity change again (online to offline or vice versa) its not working so event listener becomes dysfunctional. It just working once What I am wrong? |
Heads up: we moved react-native-netinfo into its own repository, which should allow for making changes and fixes much faster. Please continue the discussion about this issue there: react-native-netinfo/react-native-netinfo#13 |
Is this a bug report?
Yes
Have you read the Contributing Guidelines?
Yes
Environment
Android only
Environment:
OS: macOS Sierra 10.12.6
Node: 8.9.1
Yarn: 1.3.2
npm: 5.5.1
Watchman: 4.9.0
Xcode: Xcode 9.2 Build version 9C40b
Android Studio: 3.0 AI-171.4443003
Packages: (wanted => installed)
react: 16.2.0 => 16.2.0
react-native: 0.53.3 => 0.53.3
Steps to Reproduce
NetInfo.isConnected.addEventListener is not being registered unless on the root App.js component on
componentDidMount
.(Write your steps here:)
componentDidMount
orcomponentWillMount
, create an event listener.Expected Behavior
Expected the same functionality for registering an event listener on NetInfo to be the same no matter where it is called in the app.
Actual Behavior
The function specified in
NetInfo.isConnected.addEventListener
never gets called on android unless it is incomponentDidMount
on App.js.Reproducible Demo
Snippet of App.js containing the child component:
And in the NetworkStatusProvider:
The text was updated successfully, but these errors were encountered: