【问题标题】:Handler is not a function using eventChannel from redux-saga处理程序不是使用来自 redux-saga 的 eventChannel 的函数
【发布时间】:2021-01-29 14:48:58
【问题描述】:

我正在尝试使用来自 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 in const isConnected = yield take(channel);

我从here得到这个想法

【问题讨论】:

    标签: react-native redux redux-saga react-native-community-netinfo


    【解决方案1】:

    我刚刚遇到了问题

    你需要像这样调用事件监听器:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2018-01-22
      • 2019-06-14
      • 2020-05-08
      相关资源
      最近更新 更多