redux-saga - 如果使用 takeLatest,如何取消 Saga 中的任务?
问题描述
我在应用程序中有许多表单并重用相同的表单,但使用不同的“id”我创建了一个组件,这个传奇:
export default function FormSaga(SEND_REQUEST) {
return function* () {
yield takeLatest(SEND_REQUEST, submit);
};
}
但是如果我需要中止表单提交,Saga 的手册为我提供了这个解决方案:
function* main() {
while ( yield take('START_BACKGROUND_SYNC') ) {
// starts the task in the background
const bgSyncTask = yield fork(bgSync)
// wait for the user stop action
yield take('STOP_BACKGROUND_SYNC')
// user clicked stop. cancel the background task
// this will cause the forked bgSync task to jump into its finally block
yield cancel(bgSyncTask)
}
}
我试过这个:
export default function FormSaga(SEND_REQUEST, ABORT_REQUEST) {
return function* () {
while (yield take(SEND_REQUEST)) {
const submitTask = yield fork(submit);
yield take(ABORT_REQUEST);
yield cancel(submitTask);
}
};
}
但它不起作用。
如果有人可以帮助我重写我的 takeLatest 以便取消任务会起作用吗?谢谢!
解决方案
猜猜找到了解决方案,看起来它正在工作。
const task = yield takeLatest(SEND_REQUEST, submit);
yield take(ABORT_REQUEST);
yield cancel(task);
推荐阅读
- javascript - 在创建二维数组时赋值
- sql - 在构建多维数据集之前或之后最好的规范化数据是什么?
- ios - 如何在使用 pjsip 来电时唤醒 iOS 应用程序
- .net - 如何模拟 CloudBlockBlob ExistsAsync() 方法
- linux - ARM 上出现意外的 QT5 QTimer 持续时间
- django - django 自定义截断字符
- swift - UILabel 继承的 Swift 自定义 UIView
- typescript - 具有默认参数值的打字稿条件返回类型
- node.js - user.isValidPassword 不是函数
- linux - Bourne Shell 中的嵌套变量 (bin/sh)