美文网首页
React 借助redux-saga优雅实现poll轮询

React 借助redux-saga优雅实现poll轮询

作者: 秋名山车神12138 | 来源:发表于2020-04-20 16:52 被阅读0次

    轮询既可以放在组件的生命周期中开始出发,或使用setTimeout。或更高级的使用service worker,方法有很多,但在使用中想要避免在组件中引入过多这种逻辑,就可以使用redux-saga来做,非常的简单高效:

    // 使用saga/effects中的fork,在启动时自动调用
    export default function* saga() {
      yield fork(watchPollData);
    }
    
    function* watchPollData() {
      while (true) {
        yield delay(5000); // 延迟的毫秒数
        yield put(loadDataAction());
      }
    }
    

    相关文章

      网友评论

          本文标题:React 借助redux-saga优雅实现poll轮询

          本文链接:https://www.haomeiwen.com/subject/mqndihtx.html