鍍金池/ 問答/Linux  HTML/ redux-saga 不停的執(zhí)行action

redux-saga 不停的執(zhí)行action

rootSaga.js
saga總?cè)肟?/p>

import { fork, all } from "redux-saga/effects";
import { assemblyLineSaga } from "@/routes/Devops/AssemblyLine/saga";


export default function* rootSaga() {
  yield all([...assemblyLineSaga]);
  // yield sagas.map(saga => fork(saga));
}

// export default rootSaga;

2.assemblyLine/saga.js
模塊下的子saga.js

import { call, put, fork, takeLatest, takeEvery } from "redux-saga/effects";
// import { takeLatest } from "redux-saga";
import { getList } from "./service";
import * as Actions from "./actions";
function* getAssemblyLineList() {
  try {
    const assemblyLineList = yield call(getList);
    yield put({
      type: Actions.GET_ASSEMBLYLINE_LIST,
      payload: assemblyLineList
    });
  } catch (error) {
    yield put({ type: Actions.GET_ASSEMBLYLINE_FAILED, error });
  }
}
export const assemblyLineSaga = [
  takeLatest(Actions.GET_ASSEMBLYLINE_LIST, getAssemblyLineList)
];

3.action.js

export const GET_ASSEMBLYLINE_LIST = "GET_ASSEMBLYLINE_LIST";
export const GET_ASSEMBLYLINE_FAILED = "GET_ASSEMBLYLINE_FAILED";

export function getAssemblyLineList(payload) {
  return {
    type: GET_ASSEMBLYLINE_LIST,
    payload
  };
}

4.reducer.js

import * as Actions from "./actions";

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

5.view.jsx

  render() {
    return (
      <div>
        <button onClick={this.props.getAssemblyLineList}>load</button>
      </div>
    );
  }
export default connect(
  state => ({
    assemblyLine: state.assemblyLine
  }),
  { getAssemblyLineList }
)(AssemblyLine);

點(diǎn)擊按鈕的時(shí)候就一直執(zhí)行g(shù)etAssemblyLineList,如圖:

clipboard.png
找半天找不出原因。

回答
編輯回答
老梗
yield put({
      type: Actions.GET_ASSEMBLYLINE_LIST,
      payload: assemblyLineList
    });

為啥獲取數(shù)據(jù)成功時(shí)候 還是這個(gè)type,應(yīng)該一個(gè)新的type呀比如GET_ASSEMBLYLINE_SUCCEED

2017年11月15日 05:53