我正在尝试使用来自 redux saga 的 eventChannel 来监听网络状态,如下所示:
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();
}
}
但我收到以下错误TypeError: handler is not a function inconst isConnected = yield take(channel);
我从这里有了这个想法