Skip to content

Instantly share code, notes, and snippets.

@knowbody
Last active September 11, 2023 09:31
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knowbody/75b9fa1e34451ae6db1cb8bfba32f4cd to your computer and use it in GitHub Desktop.
Save knowbody/75b9fa1e34451ae6db1cb8bfba32f4cd to your computer and use it in GitHub Desktop.
Check internet connection in React Native app
// 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);
});
}
@xahon
Copy link

xahon commented Sep 15, 2017

what is 'isConnected'?

@JamesDorrian
Copy link

There is an open github issue detailing ongoing problems with .isConnected in iOS here: facebook/react-native#8615

@JamesDorrian
Copy link

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