动作必须是普通对象。使用自定义中间件
Action must be plain object. Use custom middleware
会是什么问题?
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.
配置商店:
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 中每个动作都必须 return 一个对象,这是必须的!
所以,你的 dispatch 是一个函数,应该这样调用。
此外,您只需要将 return 调度的函数声明为异步即可。 async 关键字确定以下函数将 return 一个承诺。由于您的第一个函数(菜单列表)是 return 由第二个函数(调度函数)编辑的承诺 return,因此您不必指定它。
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 })
}
}
}
会是什么问题?
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.
配置商店:
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 中每个动作都必须 return 一个对象,这是必须的! 所以,你的 dispatch 是一个函数,应该这样调用。
此外,您只需要将 return 调度的函数声明为异步即可。 async 关键字确定以下函数将 return 一个承诺。由于您的第一个函数(菜单列表)是 return 由第二个函数(调度函数)编辑的承诺 return,因此您不必指定它。
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 })
}
}
}