【问题标题】:saga is not triggered on synchronous api callsaga 不会在同步 api 调用上触发
【发布时间】:2017-08-28 19:50:30
【问题描述】:

我在我的 react redux 应用程序中使用 fetch api 以及 redux saga 问题是我在 fetch api 中有同步调用。但不,我的那个电话不是通过传奇触发的。下面是我的第一个电话

import {fetchAggregatedData1,
  fetchAggregatedData2
} from '../actions/mixActions';
fetch(types.BASE_URL + types.DATA_URL).then(response => {
    var res=response.json();
    console.log('response',res)
    const maxYear = Math.max.apply(
        Math,
        res.map(function(data) {
          return data.year;
        })
      );
      console.log('trigered in api')
      fetchAggregatedData2(maxYear);
      fetchAggregatedData1(maxYear);

    return res;

以下是我的操作

export function fetchAggregatedData2(year) {

  return {
    type: types.FETCH_AGGREGATED2,
    year
  };
}
export function fetchAggregatedData1(year) {
  return {
    type: types.FETCH_AGGREGATED1,
    year
  };
}

这是我的传奇 index.js

    export default function* startForman() {


  yield [
fork(watchAggregate1),
    fork(watchAggregate2)
    ]}

在 watchAggregate1.js 中

export default function* watchAggregate1() {
  yield takeLatest(types.FETCH_AGGREGATED1, aggregateSaga1);
}

谁能告诉我为什么同步呼叫没有通过??

【问题讨论】:

    标签: reactjs react-redux redux-saga


    【解决方案1】:

    你不应该使用没有调度功能的动作。


    在 saga 中使用 put() 来指示中间件将操作分派到 Store

    请看下面的代码,也许会有所帮助

    行动:

    const types ={
        FETCH_AGGREGATED1: 'FETCH_AGGREGATED1',
        FETCH_AGGREGATED_SUCCESS: 'FETCH_AGGREGATED_SUCCESS',
        FETCH_AGGREGATED2: 'FETCH_AGGREGATED2',
        FETCH_AGGREGATED_SUCCESS_2: 'FETCH_AGGREGATED_SUCCESS_2'
    
    
    }
    
    const fetchAggregatedData_1 = year => ({
        type: types.FETCH_AGGREGATED1,
        payload: year
    })
    
    const fetchAggregatedDataSuccess_1 =  year => ({
        type: types.FETCH_AGGREGATED_SUCCESS,
        payload:year
    })
    
    const fetchAggregatedData_2 = year => ({
        type: types.FETCH_AGGREGATED2,
        payload:year
      });
    
      const fetchAggregatedDataSuccess_2 = year => ({
        type: types.FETCH_AGGREGATED_SUCCESS_2,
        payload:year
      });
    

    api:

     fetchApi = fetch('/api').then(response => response.json())
    

    传奇:

    function*aggregateSaga1({payload}){
       try{
    
        const resp = yield call(fetchApi);
    
        /**
         * your actions with response
         * 
         */
                const maxYear = 1;
         /**
          * 
          */
    
        yield put(fetchAggregatedDataSuccess_1(maxYear));
        yield put( etchAggregatedDataSuccess_2(maxYear));
    
    
       }catch(e){
    
       }
    
        }
    
     function * watchAggregate1(){
       yield takeLatest(types.FETCH_AGGREGATED1, aggregateSaga1);
     }
    
    
    
    function* rootSaga(){
       yield[
           fork(watchAggregate1)
       ]
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      相关资源
      最近更新 更多