Home > Blockchain >  How to properly check network status inside redux saga
How to properly check network status inside redux saga

Time:11-06

Been following a couple of tutorials on Netinfo in React Native.

What I basically, wants to is to properly monitor and check the network if mobile is connected or not. If it's connected return true, if not false.

I am planning to implement this inside my redux-saga file.

So far, I created this separate file networkutils.js:

import NetInfo from '@react-native-community/netinfo';

export default class checkNetwork {
  static async isNetworkAvailable() {
    const response = await NetInfo.fetch();
    return response.isConnected;
  }
}

Then on my redux saga file:

const connectionStatus = async () => {
  await checkNetwork.isNetworkAvailable();
};
const isConnected = await connectionStatus();

console.log('status of network >>', isConnected);

This always returns undefined even if I call it inside my redux saga function like this:

export function* onAuthenticate({payload}) {
// This always returns undefined
const isConnected = yield connectionStatus();
      console.log('isConnected ??', isConnected);
      if (isConnected) {
        yield put(setUserData(userData));
        yield put(authenticateSuccess(userData));
      } else {
        yield put(authenticateError({payload: 'No Internet'}));

Any idea how do I properly check the network inside redux saga?

CodePudding user response:

Try returning it like this:

const connectionStatus = async () => {
  const status = await checkNetwork.isNetworkAvailable();
  return status;
};

See if it works.

  • Related