2017-08-28 84 views
0

我在我的反應REDX應用程序中使用提取API以及REDX傳奇流問題是我有同步調用訪存API。但是我的那個電話不會通過傳奇觸發。下面是我的第一個電話傳奇沒有觸發同步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); 
} 

任何人都可以讓我知道爲什麼同步呼叫不是經歷?

回答

0

不應該使用沒有調度功能的操作。


在指示中間件以下派遣一個動作到店

請參見代碼傳奇使用的put(),也許這將是有益的

操作:

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) 
    ] 

}