简体   繁体   English

Redux Saga 在任何错误后都无法工作。 它完全停止工作,需要刷新页面

[英]Redux Saga does not work after any error. It totally stops working and need to refresh the page

Redux Saga does not work after any error. Redux Saga 在任何错误后都无法工作。 It totally stops working and need to refresh the page.它完全停止工作,需要刷新页面。 Working well when does not generate any error.运行良好时不会产生任何错误。

I want whether any error occurred the page should running if click the process again and fetch data from API.我想如果再次单击该进程并从 API 获取数据,页面是否应该运行,是否发生任何错误。

If anyone gives any suggestion/solution, please.如果有人提出任何建议/解决方案,请。

My axios call as like below,我的 axios 调用如下,

export async function getWithPagination(pageNumber, pageSize, searchObject) {
  try {
    const response = await axios.get(
      uri.ApiBaseUrl + "Voucher/GetWithPagination",
      {
        params: {
          ...searchObject,
          pageNumber,
          pageSize,
        },
      }
    );
    return { response };
  } catch (error) {
    return { error };
  }
}

export async function createVoucher(voucher) {
  console.log("createVoucher service=> ", voucher);
  try {
    const response = await axios.post(uri.ApiBaseUrl + "Voucher", voucher, {});
    return { response };
  } catch (error) {
    return { error };
  }
}

The reducer, action, saga like below减速器,动作,传奇如下

import { put, takeLatest, call } from "redux-saga/effects";
import {
  getWithPagination,  
  createVoucher  
} from "../../services-crud/accounting/vouchersCrud";

export const reducer = (state = voucherState, action) => {
    case actionTypes.VoucherGetRequested: {
      return {
        ...state,
        voucherGridObj: {
          paging: {},
          links: {},
          lists: [],
        },
        isGridLoading: true,
      };
    }
    case actionTypes.VoucherGetSucceed: {
      return { ...state, voucherGridObj: action.data, isGridLoading: false };
    }
    case actionTypes.VoucherGetFailed: {
      return { ...state, isGridLoading: false };
    }
    case actionTypes.VoucherCreateRequested: {
      return { ...state };
    }
    case actionTypes.VoucherCreateSucceed: {
      return {
        ...state,
        voucherObj: { ...voucherInit },
        voucherDetailsList: [],
        voucherGridObj: {
          ...state.voucherGridObj,
          lists: [{ ...action.data.voucher }, ...state.voucherGridObj.lists],
        },
        isSuccess: false,
        isDataSyncNeeded: true,
      };
    }
    case actionTypes.VoucherCreateFailed: {
      return { ...state, isSuccess: false, isDataSyncNeeded: false };
    }
}

export const actions = {
     voucherGetRequested: (pageNumber, pageSize, searchObject) => ({
    type: actionTypes.VoucherGetRequested,
    pageNumber: pageNumber,
    pageSize: pageSize,
    searchObject: searchObject,
  }),
  voucherGetSucceed: (data) => ({
    type: actionTypes.VoucherGetSucceed,
    data: data,
  }),
  voucherGetFailed: () => ({ type: actionTypes.VoucherGetFailed }),

  voucherCreate: (voucherObj, voucherImage, gurdianImage) => ({
    type: actionTypes.VoucherCreate,
    voucherObj: voucherObj,
    voucherImage: voucherImage,
    gurdianImage: gurdianImage,
  }),
  voucherCreateRequested: () => ({
    type: actionTypes.VoucherCreateRequested,
  }),
  voucherCreateSucceed: (data) => ({
    type: actionTypes.VoucherCreateSucceed,
    data: data,
  }),
  voucherCreateFailed: () => ({
    type: actionTypes.VoucherCreateFailed,
  }),
};

export function* saga() {

    try {
    yield takeLatest(
      actionTypes.VoucherGetRequested,
      function* voucherRequested(action) {
        const { response, error } = yield call(() =>
          getWithPagination(
            action.pageNumber,
            action.pageSize,
            action.searchObject
          )
        );

        if (response) {
          yield put(actions.voucherGetSucceed(response.data));
        } else {
          NotificationMessage(errorResponseProcess(error.response));
          yield put(actions.voucherGetFailed(error));
        }
      }
    );
  } catch (error) {
    NotificationMessage(errorResponseProcess(error.response));
    yield put(actions.voucherGetFailed(error));
  }

   try {
    yield takeLatest(
      actionTypes.VoucherCreate,
      function* createsvoucher(action) {
        yield put(actions.voucherCreateRequested());
        const { response, error } = yield call(() =>
          createVoucher(
            action.voucherObj,
            action.voucherImage,
            action.gurdianImage
          )
        );
        if (response) {
          NotificationMessage(response.data.notification);

          yield put(actions.voucherCreateSucceed(response.data.data));
          yield put(actions.voucherResetFlag());
        } else {
          NotificationMessage(errorResponseProcess(error.response));
          yield put(actions.voucherCreateFailed(error));
        }
      }
    );
  } catch (error) {
    NotificationMessage(errorResponseProcess(error.response));
    yield put(actions.voucherCreateFailed(error));
  }

}

The root reducer & store is below根减速器和存储在下面

import { all } from "redux-saga/effects";
import createSagaMiddleware from "redux-saga";
import { combineReducers, applyMiddleware, createStore } from "redux";

import * as accountsReportCrud from "./accounts-chart-duck/accountsReportCrudDuck";
import * as vouchersCrud from "./voucher-duck/voucherCrudDuck";

export const rootReducer = combineReducers({

  accountsCrud: accountsCrud.reducer,
  accountsReportCrud: accountsReportCrud.reducer,

});

export function* rootSaga() {
  yield all([

  accountsCrud.saga(),
  accountsReportCrud.saga(),
   ...
  
  ]);
}

const sagaMiddleware = createSagaMiddleware();
const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));

sagaMiddleware.run(rootSaga);

export default store;

In your root saga, you can spawn the tasks instead of all / fork so that one saga's error doesn't crash all the sagas.在您的 root saga 中,您可以spawn任务而不是all / fork这样一个 saga 的错误就不会导致所有的 saga 崩溃。

Redux-saga docs on detached fork (spawn) : Redux-saga 文档关于分离叉 (spawn)

Uncaught errors from spawned tasks are not bubbled up to the parent.来自衍生任务的未捕获错误不会冒泡到父级。

I think this formation of sagas will does the trick我认为这种传奇的形成可以解决问题

function* function_name({ payload }){
  //code here
}

export function* function_name_sagas() {
    yield takeEvery(action_type, function_name);
}

export default function* rootSaga() {
    yield all([
        fork(function_name_sagas),
    ]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM