Last active
September 11, 2023 09:31
-
-
Save knowbody/75b9fa1e34451ae6db1cb8bfba32f4cd to your computer and use it in GitHub Desktop.
Check internet connection in React Native app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// quick snippet to check the connection in your RN app | |
// dispatches an `setIsConnected` action every time the NetInfo changes (on/off network) | |
componentDidMount() { | |
const dispatchConnected = isConnected => this.props.dispatch(setIsConnected(isConnected)); | |
NetInfo.isConnected.fetch().then().done(() => { | |
NetInfo.isConnected.addEventListener('change', dispatchConnected); | |
}); | |
} |
There is an open github issue detailing ongoing problems with .isConnected in iOS here: facebook/react-native#8615
I created a solution and put it in a repo here: https://github.com/JamesDorrian/NetInfoCheckConnection/tree/master
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is 'isConnected'?