【发布时间】:2020-03-30 19:00:34
【问题描述】:
我有一个networkSaga,我可以在其中获取帖子、添加和删除喜欢的内容。添加或删除点赞后,我需要致电getPosts 更新点赞数。
在 redux-thunk 中,我会在添加或删除类似之后简单地调用 dispatch(getPosts())。由于我是 sagas 的新手,我很担心,应该怎么做?
import { all, call, fork, put, takeEvery } from 'redux-saga/effects';
import { NetworkActionTypes } from './types';
import { apiCaller } from '../../utils/apiCaller';
import { onSuccess, onFailure } from '../../utils/actionCreators';
function* getPosts(action): Generator {
try {
const res = yield call(apiCaller, action.meta.method, action.meta.route, action.meta.data);
yield put(onSuccess(NetworkActionTypes.GET_POSTS_SUCCESS, res));
} catch (err) {
yield put(onFailure(NetworkActionTypes.GET_POSTS_ERROR, err));
}
}
function* addLike(action): Generator {
try {
const res = yield call(apiCaller, action.meta.method, action.meta.route, action.meta.data);
yield put(onSuccess(NetworkActionTypes.ADD_LIKE_SUCCESS, res));
} catch (err) {
yield put(onFailure(NetworkActionTypes.ADD_LIKE_ERROR, err));
}
}
function* removeLike(action): Generator {
try {
const res = yield call(apiCaller, action.meta.method, action.meta.route, action.meta.data);
yield put(onSuccess(NetworkActionTypes.REMOVE_LIKE_SUCCESS, res));
} catch (err) {
yield put(onFailure(NetworkActionTypes.REMOVE_LIKE_ERROR, err));
}
}
/**
* @desc Watches every specified action and runs effect method and passes action args to it
*/
function* watchFetchRequest(): Generator {
yield takeEvery(NetworkActionTypes.GET_POSTS, getPosts);
yield takeEvery(NetworkActionTypes.ADD_LIKE, addLike);
yield takeEvery(NetworkActionTypes.REMOVE_LIKE, removeLike);
}
/**
* @desc saga init, forks in effects, other sagas
*/
export function* networkSaga() {
yield all([fork(watchFetchRequest)]);
}
这个问题可能听起来很愚蠢,但如果您指出解决方案,我将不胜感激。谢谢!
【问题讨论】:
-
所以基本上你想在当前的成功后再次调用生成器函数对吗?
-
@PrabhatMishra 是的,在我成功后
addLike我需要打电话给getPosts
标签: reactjs redux redux-saga