Search code examples
javascriptreactjsreduxreact-reduxredux-saga

How do I get the state in a function?


I have two files, post.js and index.js.

post.js manages initialState and index handles saga's api.

But what if I want to get the state every time an action is executed in getPost?

For example, if I want to check console.log(getPostloading) in getPost catch (err) { } when GETPOST_REQUEST, or GETPOST_SUCCESS or GETPOST_FAILURE action is executed, how do I change the code?

this is my code

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

Solution

  • If you want using state in redux-saga, you can use select: https://redux-saga.js.org/docs/api/#selectselector-args

    example:

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