【发布时间】:2020-10-10 09:29:26
【问题描述】:
我正在尝试使用 redux-saga 实现取消任务效果。它在第一次调度时运行良好,但在第二次调度时,它什么也不做。看来传奇已经结束了。这是我的代码:
import { all, put, fork, take, cancel, cancelled } from "redux-saga/effects";
const searchUsers = function* () {
try {
yield new Promise((resolve) => setTimeout(resolve, 1500));
const users = ["Bill Gates"];
yield put({ type: "SEARCH_USERS_SUCCESS", users });
} catch (e) {
// log error
} finally {
if (yield cancelled()) {
console.log("search was cancelled");
}
}
};
const searchUsersSaga = function* () {
const searchAction = yield take("SEARCH_USERS");
const searchTask = yield fork(searchUsers, searchAction.query);
const cancleAction = yield take("SEARCH_USERS_CANCEL");
if (cancleAction.type === "SEARCH_USERS_CANCEL") {
yield cancel(searchTask);
}
};
const rootSaga = function* saga() {
yield all([searchUsersSaga].map(fork));
};
export default rootSaga;
我在这里创建了一个完整的代码:https://codesandbox.io/s/new-fast-snhr0?file=/src/index.js
【问题讨论】:
标签: reactjs react-redux redux-saga