我正在使用redux-saga。 我的传奇实现只需要调用一次就可以调用两次。
我的传奇:
export function* api(parms) {
try {
//when parms hasnt a uv key, it's a local calling
if(!parms['uv']){
yield put(publish(parms,parms['action']));
return
}
//to check user_id if the user is login - from local storage
if(isLogin()){
if(!parms['user_id'])
parms['user_id'] = getData('USER').user_id;
}
//to create token
parms['tok'] = getToken(parms,parms['uv'])
parms['time'] = todayForamted;
var formData = new FormData()
for ( var key in parms ) {
formData.append(key, parms[key]);
}
var requestOptions = {}
requestOptions.body = formData
// Call our request helper (see 'utils/request')
const response = yield call(request, baseUrl+parms['uv'], requestOptions);
yield put(done(response,parms['uv']));
} catch (err) {
console.log('error',err)
yield put(error(err));
}
}
/**
* Root saga manages watcher lifecycle
*/
export default function* watcherApi() {
yield takeLatest([Uva2,Uva3,Uva8,Uva10,Uva11,Uva19,Uva20,Uva46,current_tr], api);
}
[Uva2,Uva3,Uva8,Uva10,Uva11,Uva19,Uva20,Uva46,current_tr]
是我的唯一键。
我的动作:
export function getFinanceOfUsersByUserId(parms) {
return {
type: Uva19,
...parms,
};
}
export function done(res,uv) {
return {
type: DONE,
uv:uv,
...res,
};
}
export function error(error) {
return {
type: ERROR,
error,
};
}
我的组件连接:
const mapStateToProps = (state, ownProps) => {
var responsiveObj = state.get('api').get('responsiveObj')
if(!responsiveObj)
return {}
else
return {'responsiveObj':responsiveObj}
}
const withConnect = connect(
mapStateToProps,
// mapDispatchToProps,
);
const withReducer = injectReducer({ key: 'api', 'reducer':apiReducer });
const withSaga = injectSaga({ key: 'api', saga });
export default compose(
withReducer,
withSaga,
withConnect,
)(Index);
在我的componentDidMount
函数中:
this.props.dispatch(getFinanceOfUsersByUserId({'uv':'Uva19'}))
我所有的肌动蛋白都叫了两次!