【发布时间】:2021-05-10 15:25:06
【问题描述】:
我有两个文件,post.js 和 index.js。
post.js 管理 initialState,index 处理 saga 的 api。
但是,如果我想在 getPost 中每次执行操作时获取状态怎么办?
例如,如果我想在执行 GETPOST_REQUEST 或 GETPOST_SUCCESS 或 GETPOST_FAILURE 操作时检查 getPost catch (err) { } 中的 console.log(getPostloading),我该如何更改代码?
这是我的代码
(reducer/post.js)
(reducer/post.js)
import produce from '../util/produce';
export const initialState = {
getPostinitial: null,
getPostloading: false,
getPostsuccess: false,
getPosterror: false,
};
export const GETPOST_REQUEST = 'GETPOST_REQUEST';
export const GETPOST_SUCCESS = 'GETPOST_SUCCESS';
export const GETPOST_FAILURE = 'GETPOST_FAILURE';
const reducer = (state = initialState, action) =>
produce(state, (draft) => {
switch (action.type) {
case GETPOST_REQUEST:
// dratf.getPostinitial= false,
draft.getPostloading = true;
draft.getPostsuccess = false;
draft.getPosterror = false;
draft.loadPostError = null;
break;
case GETPOST_SUCCESS:
draft.getPostloading = false;
draft.getPostsuccess = true;
draft.getPosterror = false;
draft.loadPostDone = true;
break;
case GETPOST_FAILURE:
draft.getPostloading = false;
draft.getPostsuccess = false;
draft.getPosterror = true;
draft.loadPostError = action.error;
break;
}
});
export default reducer;
(传奇/index.js)
import {
GETPOST_FAILURE,
GETPOST_REQUEST,
GETPOST_SUCCESS,
} from '../reducers/post';
function getPostAPI(data) {
return axiosInstace.post('/kakao/getpost', data);
}
function* getPost(action) {
try {
const result = yield call(getPostAPI, action.data);
yield put({
type: GETPOST_SUCCESS,
data: result.data,
});
} catch (err) {
console.log(getPostloading) // i want to check getPostloading state value
if (err.response.data === 'jwtEx') {
yield call(refresh);
yield put(action);
} else {
yield put({
type: GETPOST_FAILURE,
error: err.response.data,
});
}
}
}
function* watchgetPost() {
yield takeLatest(GETPOST_REQUEST, getPost);
}
export default function* postSaga() {
yield all([fork(watchgetPost)]);
}
【问题讨论】:
标签: javascript reactjs redux react-redux redux-saga