reactjs - 如果找不到页面,Redux Saga 将继续循环
问题描述
这是我的rootSaga.ts
import { all } from 'redux-saga/effects';
import { Watcher } from '../sagas';
export default function* rootSaga() {
yield all([Watcher()]);
}
我的API axios
import axios from 'axios';
const fetchAPI = () => {
return axios.get('https://sample404Case');
};
export default fetchAPI;
这是我的Saga
export function* Watcher() {
yield takeLatest(actionIds.FETCH_SAMPLE_REQUEST_START, fetchSample);
}
function* fetchSample() {
try {
console.log('generated');
const generatedSample = yield call(FetchSample);
console.log(generatedSample);
yield put({ type: actionIds.FETCH_SAMPLE_REQUEST_SUCCESSFUL, payload: generatedSample.data });
} catch (error) {
console.log(error);
yield put({ type: actionIds.FETCH_SAMPLE_REQUEST_START, error });
}
}
注意我故意想要一个 404 错误,但我的问题是它会产生一个 continue 循环,这会使我的页面冻结。
一旦出现错误,有没有办法停止传奇?
解决方案
当你有一个错误导致它进入无限循环时,你正在重新启动 saga(因为你希望它出现 404 错误):
catch (error) {
console.log(error);
yield put({ type: actionIds.FETCH_SAMPLE_REQUEST_START, error });
}
您应该为失败状态添加另一个操作(例如:FETCH_SAMPLE_REQUEST_FAILED)并在 catch 内调度它
推荐阅读
- python-3.x - 比较 2 个文件的列,将一个文件中不同列中的值替换为另一个文件中 1 列中的值 Python
- python - Snapdragon神经处理引擎安装
- c++ - Why can I not use a constexpr global variable to initialize a constexpr reference type?
- javascript - 固定 div 但在页脚之前停止
- sql - Calculating Percentage of Various Categories that Meet Conditions with SQL Server
- python - 根据某些条件合并excel工作表
- java - I have a problem with a leaked window that was originally added here after the hide in onDoInBackground show file update
- azure - .NET Core Azure Function 部署失败
- r - 在管道中分配变量并继续?
- angular - Angular - 显示加载微调器,直到方法完成执行