![](/img/trans.png)
[英]Async Action Redux Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions
[英]Action must be plain object. Use custom middleware
有什么問題嗎?
未捕獲的錯誤:動作必須是普通對象。 使用自定義中間件進行異步操作。
配置商店:
export default configureStore = () => {
let store = compose(applyMiddleware(ReduxThunk))(createStore)(reducers);
return store;
}
行動
export const menulist = async ({ navigation }) => {
return async dispatch => {
try {
dispatch({ type: types.MENULIST_REQUEST_START })
let response = await menuListByCategories();
dispatch({ type: types.MENULIST_SUCCESS })
} catch (error) {
dispatch({ type: types.MENULIST_FAILED })
}
}
}
您使用錯誤的方式,
在Redux中,每個動作都必須返回一個對象,這是必須的! 因此,應以這種方式調用作為函數的調度。
此外,您只需要聲明異步返回派遣的功能。 async關鍵字確定以下函數將返回承諾。 當您的第一個函數(菜單員)返回第二個函數(調度一個)返回的promise時,您不必指定它。
export const menulist = ({ navigation }) => async (dispatch) => {
try {
dispatch({ type: types.MENULIST_REQUEST_START })
let response = await menuListByCategories();
dispatch({ type: types.MENULIST_SUCCESS })
} catch (error) {
dispatch({ type: types.MENULIST_FAILED })
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.