Handler is not a function using eventChannel from redux-saga

434 Views Asked by At

I'm trying to listen the network status using eventChannel from redux saga like this:

import {put, take} from 'redux-saga/effects';
import {eventChannel} from 'redux-saga';
import NetInfo from '@react-native-community/netinfo';

export function* startWatchingNetworkConnectivity() {
  const channel = eventChannel((emitter) => {
    NetInfo.addEventListener('connectionChange', emitter);
    return () => NetInfo.removeEventListener('connectionChange', emitter);
  });

  try {
    while (true) {
      const isConnected = yield take(channel);

      if (isConnected) {
        yield put({type: 'ONLINE'});
      } else {
        yield put({type: 'OFFLINE'});
      }
    }
  } finally {
    channel.close();
  }
}

But I'm getting the following error TypeError: handler is not a function in const isConnected = yield take(channel);

I've got this idea from here

1

There are 1 best solutions below

0
On

I've just got the problem

You need to call the event listener like this:

  const channel = eventChannel((emitter) => {
    const unsubcribe = NetInfo.addEventListener((state) =>
      emitter(state.isConnected),
    );
    return () => unsubcribe();
  });