【问题标题】:Use yield inside of setTimeOut in redux saga在 redux saga 中使用 setTimeOut 内的 yield
【发布时间】:2020-01-17 08:41:33
【问题描述】:

在我的 React 项目中,我有以下代码:

export function* onDisplayAlert({ payload }: any) {
  payload.id = getUniqueID();
  yield put(setAlert(payload));

  yield setTimeout(() => {
    yield put(removeAlert(payload.id));
  }, 3000);
}

我在这里要做的是在setTimeOut 回调中使用yield

yield put(removeAlert(payload.id));

但是我写这个的方式不行,因为箭头函数回调不是生成器函数,所以不能在里面使用yield。如何在setTimeOut 中使用yield?

【问题讨论】:

  • 请解释一下我们没有明白你的意思
  • 你想达到什么目的?你如何使用onDisplayAlert
  • @alex2007v 的答案不适合你吗?

标签: javascript reactjs typescript react-native redux-saga


【解决方案1】:

这就是你需要的

export function* onDisplayAlert({ payload }: any) {
  payload.id = getUniqueID();
  yield put(setAlert(payload));
  yield delay(3000);
  yield put(removeAlert(payload.id));
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-02-01
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
  • 2017-06-14
  • 1970-01-01
相关资源
最近更新 更多