-
-
Save williamtran29/4c2f8e9647b831f9f89bb95e71b075f6 to your computer and use it in GitHub Desktop.
Check internet connection in React Native app
This file contains 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); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment