[英]Action must be plain object. Use custom middleware
What would be the problem? 有什么问题吗?
Uncaught Error: Actions must be plain objects.
未捕获的错误:动作必须是普通对象。 Use custom middleware for async actions.
使用自定义中间件进行异步操作。
Configure Store: 配置商店:
export default configureStore = () => {
let store = compose(applyMiddleware(ReduxThunk))(createStore)(reducers);
return store;
}
Action 行动
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 })
}
}
}
You are using it the wrong way, 您使用错误的方式,
in Redux every action must return an object, and this is a must! 在Redux中,每个动作都必须返回一个对象,这是必须的! so, your dispatch, which is a function, should be called this way.
因此,应以这种方式调用作为函数的调度。
Besides you only need to declare async the function which returns dispatch. 此外,您只需要声明异步返回派遣的功能。 The async keyword determines that the following function will return a promise.
async关键字确定以下函数将返回承诺。 As your first function (menulist) is returning the promise returned by the second function (dispatch one) you don't have to specify it.
当您的第一个函数(菜单员)返回第二个函数(调度一个)返回的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.