How to handle network failure in React-Native, when network is off

rakesh r picture rakesh r · Apr 29, 2016 · Viewed 14.1k times · Source

How to handle network failure in React-Native, when device not connected to network.

My scenario is am trying to connect some api, while fetching request if network is disconnected react-native throws network request failed error. how to handle this scenario to give the user best user experience.

How to handle Network request failed in a better way.

enter image description here

Answer

Ryan Marshall picture Ryan Marshall · Apr 29, 2016

Use NetInfo like this:

// Check for network connectivity
NetInfo.isConnected.fetch().done((isConnected) => {
    if ( isConnected )
    {
        // Run your API call
    }
    else
    {
        // Ideally load from local storage
    }
});