在redux thunk中没有返回Promise

问题描述 投票:0回答:2

我是react-redux的新手。我在这里做的是,

我有一个动作就像

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

现在,在我的容器中,我想调用一个在此之后调用的动作,

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
      }
    }

所以,在这里我试图使用then,但我得到一个错误,Uncaught TypeError: _this.props.updateActivePage(...).then is not a function

那么我做错了什么呢?

reactjs react-native redux react-redux redux-thunk
2个回答
1
投票

你从根本上滥用了redux-thunk。此库的预期用途是调度一个动作创建者(在本例中为updateActivePage),它返回一个用状态参数调用的函数。你应该派遣updateActivePage,因为这是你的thunk。此外,这是应该包含您的计算的函数,然后应该将结果置于状态。您似乎直接调用此函数,并且出于某种原因期望它返回一个promise,然后以某种方式进行一些计算。你应该重新访问thunk文档。


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

派遣不是承诺,它只是一个功能。

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

你可以尝试像我上面提到的那样回复承诺吗?

© www.soinside.com 2019 - 2024. All rights reserved.