What is the problem with redux-saga triggering action multiple times?

1.rootSaga.js

import { fork, all } from "redux-saga/effects";
// import sub-sagas from submodules
import { assemblyLineSaga } from "@/routes/Devops/AssemblyLine/saga";
import { DashboardSaga } from "@/routes/Devops/Dashboard/saga";


export default function* rootSaga() {
  yield all([...assemblyLineSaga, ...DashboardSaga]);
}

2.dashboard/actions.js

export const GET_ALLDATA_SRART = "GET_ALLDATA_SRART";
export const GET_ALLDATA_SUCCESS = "GET_ALLDATA_SUCCESS";
export const GET_ALLDATA_FAILED = "GET_ALLDATA_FAILED";
export function getAllList(payload) {
 return {
  type: GET_ALLDATA_SUCCESS,
  payload
 };
}

3.dashboard/reducer.js

const initialState = {};
export default (state = initialState, action) => {
  switch (action.type) {
   case Actions.GET_ALLDATA_FAILED:
  return state;
   case Actions.GET_ALLDATA_SUCCESS:
  return {
    ...state,
    ...action.payload
  };
   case Actions.GET_ALLDATA_FAILED:
     return state;
  default:
     return state;
 }
};

4.dashboard/saga.js

import { call, put, fork, takeLatest, takeEvery } from "redux-saga/effects";
import { getList } from "./service";
import * as Actions from "./actions";
function* getAllList() {
try {
 yield put({
  type: Actions.GET_ALLDATA_SRART,
});
const list = yield call(getList);
 yield put({
  type: Actions.GET_ALLDATA_SUCCESS,
  payload: list
 });
} catch (error) {
  yield put({ type: Actions.GET_ALLDATA_FAILED, error });
  }
}
export const DashboardSaga = [fork(getAllList)];

5.dashboard/service.js

import Http from "@/utils/http";
export const getList = () => {
 return Http.get(
  "https://api.github.com/search/repositories?q=javascript&sort=stars"
 );
};

6.dashboard/view.js

import { connect } from "react-redux";
import { getAllList } from "./actions";
...
componentDidMount() {
  this.props.getAllList()
}
export default connect(state => ({ state }), { getAllList })(Dashboard);

found that the order of calling action is dispatch "GET_ALLDATA_SRART" = > "GET_ALLDATA_SUCCESS" = > "GET_ALLDATA_SUCCESS" and the first "GET_ALLDATA_SUCCESS" returns payload is undefined.

Feb.28,2021

. It's a mess,

componentDidMount() {
  this.props.getAllList()
}

if you remove this thing, you will find that "GET_ALLDATA_SRART" = > "GET_ALLDATA_SUCCESS" will still be executed.

Let's get through the train of thought before we write.

action returned by getAllList in

(dashboard/actions.js) should be GET_ALLDATA_SRART , and then do not execute GET_ALLDATA_SRART in try catch of your (dashboard/saga.js) getAllList . What you need to do is to use takeLatest or takeEvery to listen GET_ALLDATA_SRART this type action to execute your < hr >

start is not srart , and you don't need to set the function names to the same, so many getAllList look dizzy.

Menu