[英]Call an Async action after updating the react state
我是新來的react js
。 我有一個動作,
export const updateActivePage = (activePage) => {
return {
type: UPDATE_ACTIVEPAGE,
payload: activePage
}
}
在我的減速機中,
const initialState = {
activePage: 1,
}
case UPDATE_ACTIVEPAGE: {
return {
...state,
activePage: activePage
}
}
在我的容器里,
handlePageChange = (pageNumber) => {
this.props.updateActivePage(pageNumber);
this.props.fetchUserJd(this.props.activePage);
}
現在我要調用this.props.fetchUserJd(this.props.activePage);
僅在前一個操作成功更新該值時才起作用。 因為這沒有回應任何承諾。
現在,我有一個回調函數的解決方案。 但不知何故.then
不作為第一個函數沒有返回任何承諾在本avaliable。 任何幫助都會有所幫助。
您可以執行以下操作:在mapDispatchToProps中返回如下操作:
function mapDispatchToProps(dispatch) {
return {
updateActivePage: (pageNumber) => {
return new Promise((resolve, reject) => {
dispatch(actions.updateVisualState(pageNumber, resolve, reject));
});
}
};
在這種情況下,您將能夠在視圖中使用then進行操作:
this.props.updateActivePage(pageNumber).then(()=>{
this.props.fetchUserJd(this.props.activePage);
})
您只需選擇正確的地方來解決承諾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.