Reactjs 在redux saga中使用回调时出现的问题
我有一个函数(名为Reactjs 在redux saga中使用回调时出现的问题,reactjs,typescript,redux,redux-saga,Reactjs,Typescript,Redux,Redux Saga,我有一个函数(名为onGetCameras)接受回调函数(例如getCamerasSuccess);其目的是调用onGetCameras(这是一个外部函数),执行AJAX调用,完成后调用getCamerasSuccess,传递接收到的结果 我拥有的是这样的东西: // My 'actionCreators' variable is a reference to create my actions function* getCamerasSaga(action: action.GetCameras
onGetCameras
)接受回调函数(例如getCamerasSuccess
);其目的是调用onGetCameras
(这是一个外部函数),执行AJAX调用,完成后调用getCamerasSuccess
,传递接收到的结果
我拥有的是这样的东西:
// My 'actionCreators' variable is a reference to create my actions
function* getCamerasSaga(action: action.GetCameras) {
const { onGetCameras } = action;
const getCamerasSuccess = function*(response: GetCamerasResponse) {
console.log('SUCCESS', response);
yield put(actionCreators.getCamerasSuccess(response);
}
yield put(actionCreators.getCamerasStart());
yield call(onGetCameras, getCamerasSuccss);
}
export function* watchCamera() {
yield takeEvery(ActionTypes.GetCameras, getCamerasSaga);
}
function* getCamerasSaga(action: action.GetCameras) {
const { onGetCameras } = action;
yield put(actionCreators.getCamerasStart());
try {
const response: GetCamerasResponse = yield cps(onGetCameras);
console.log('SUCCESS', response);
yield put(actionCreators.getCamerasSuccess(response));
} catch(err) { /* ... */ }
}
我不明白为什么它没有进入我的getCamerasSuccess
函数:我从来没有在该函数中看到我的控制台.log
消息
但是,如果我将成功回调更改为普通函数,例如:
const getCamerasSuccess = (response: GetCamerasResponse) => {
console.log('RESPONSE', response);
}
我可以看到我收到了我的响应,但是,正如我提到的,使用生成器函数,它似乎从未进入该函数的内部
非常感谢您的帮助。您的
getCamerasSuccess
不会被调用,因为当您调用函数时,它会被执行,但当您调用生成器函数时,它只返回一个迭代器对象,您必须继续调用该对象才能执行
不过,您的代码仍然无法工作,因为您试图在不受redux saga控制的生成器中使用saga效果。如果您想继续使用回调,您可能会对cps效应()感兴趣。回调必须是node.js样式(第一个参数err,第二个结果)
然后,您的代码可能如下所示:
// My 'actionCreators' variable is a reference to create my actions
function* getCamerasSaga(action: action.GetCameras) {
const { onGetCameras } = action;
const getCamerasSuccess = function*(response: GetCamerasResponse) {
console.log('SUCCESS', response);
yield put(actionCreators.getCamerasSuccess(response);
}
yield put(actionCreators.getCamerasStart());
yield call(onGetCameras, getCamerasSuccss);
}
export function* watchCamera() {
yield takeEvery(ActionTypes.GetCameras, getCamerasSaga);
}
function* getCamerasSaga(action: action.GetCameras) {
const { onGetCameras } = action;
yield put(actionCreators.getCamerasStart());
try {
const response: GetCamerasResponse = yield cps(onGetCameras);
console.log('SUCCESS', response);
yield put(actionCreators.getCamerasSuccess(response));
} catch(err) { /* ... */ }
}
如果修改onGetCameras
API不是一个选项,您必须使用普通函数作为回调,然后使用store.dispatch
而不是put
,或者您可以使用eventChannel()创建一些小的实用函数
例如:
非常感谢@Martin的回答
cps
似乎对我起了作用。只有一个问题:我使用的是TypeScript,那么onGetCameras的类型应该是什么呢?因为现在,当我生成cps(onGetCameras)
时,它抱怨我给它的类型中缺少属性上下文
,为了让它工作,我这样做了:生成cps(onGetCameras.bind(null))
,但我不确定这是正确的方法我对typescript几乎没有经验,但快速谷歌搜索发现a)如果一个函数有多个类型定义,但你失败了,那么typescript将只显示最后一个(可能解释为什么它抱怨上下文)b)这是一个cps回调接口:interface CpsCallback{(错误:any,结果:R):void;cancel?():void;}
无论如何,感谢您的原始答案,它帮助很大:)