Anderson Anderson - 11 days ago 10x
React JSX Question

React Native no internet fetch crashes

I'm using

React Native
for an chat android app.

In my application I have an action which fetch the api like this:


This will be called in the chat component lifecycle

Also In my app in the root container I check if I have internet connection, if not I show a modal above everything.

My problem is like this: if the chat component mounts and I lose my internet connection then app crash.

I'm doing something wrong? How can I stop fetch if I dont have no internet connection?


try to check the connection everytime it goes off and on where you call getMessages. With this snippet you can check when the connection goes on and off:

componentDidMount() {
  const dispatchConnected = isConnected => this.props.dispatch(setIsConnected(isConnected));

  NetInfo.isConnected.fetch().then().done(() => {
    NetInfo.isConnected.addEventListener('change', dispatchConnected);