如何获取函数中的状态?

How do I get the state in a function?

我有两个文件,post.js 和 index.js。

post.js 管理 initialState,index 处理 saga 的 api.

但是如果我想在getPost中每次执行动作时获取状态怎么办?

例如,如果我想检查 console.log(getPostloading) in getPost catch (err) { } when GETPOST_REQUEST, or GETPOST_SUCCESS or GETPOST_FAILURE action is已执行,如何更改代码?

这是我的代码

(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;

(saga/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)]);
        }

如果你想在redux-saga中使用state,你可以使用selecthttps://redux-saga.js.org/docs/api/#selectselector-args

示例:

catch (err) {
    const getPostloading = yield select(state => state.post.getPostloading);
    console.log(getPostloading)
...