我在我的react redux应用程序中使用fetch api以及redux saga ow问题我在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)
]}
in watchAggregate1.js
export default function* watchAggregate1() {
yield takeLatest(types.FETCH_AGGREGATED1, aggregateSaga1);
}
任何人都可以告诉我为什么同步电话没有通过?
答案 0 :(得分:0)
如果没有调度功能,你不应该使用行动。
在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)
]
}