ganesh kaspate
ganesh kaspate

Reputation: 2685

Promise is not getting returned in the redux thunk

I am new to react-redux. Here what I am doing is that,

I have an action that is like

export const updateActivePage = (activePage) => {
  return (dispatch) => {
    return dispatch ({
      type: UPDATE_ACTIVEPAGE,
      payload: activePage
    });
  }
}

Now, in my container, I want to call an action which will get called after this so,

handlePageChange = (pageNumber) => {
    this.props.updateActivePage(pageNumber).then(() => {
      this.props.fetchUserJd(this.props.activePage);
    })
  }



case UPDATE_ACTIVEPAGE:  {
      console.log("action payload", action.payload);
      return {
        ...state,
        activePage: action.payload
      }
    }

So, Here I am trying to use then but I am getting an error that Uncaught TypeError: _this.props.updateActivePage(...).then is not a function

So what is it that I am doing wrong?

Upvotes: 0

Views: 113

Answers (2)

Raja Sekar
Raja Sekar

Reputation: 2130

export const updateActivePage = (activePage) => {
  return (dispatch) => {
    return dispatch ({
      type: UPDATE_ACTIVEPAGE,
      payload: activePage
    });
  }
}

dispatch is not a promise, It just a function.

export const updateActivePage = (activePage) => {
  return (dispatch) => {
    return new Promise((res, rej) => {
      dispatch ({
        type: UPDATE_ACTIVEPAGE,
        payload: activePage
      });
      res(true);
    })
  }
}

Can you try returning promise like I mentioned above?

Upvotes: 0

lluisrojass
lluisrojass

Reputation: 439

You are fundamentally misusing redux-thunk. The intended use for this library is to dispatch an action creator (in this case, updateActivePage), which returns a function that gets invoked with state params. You should be dispatching updateActivePage since this is your thunk. Also, this is the function which should contain your computation, which should then posit the results into state. It appears that you are calling this function directly, and for some reason expect it to return a promise, and then somehow doing some computation. You should re-visit the thunk docs.

Upvotes: 1

Related Questions