繁体   English   中英

动作必须是普通对象。 使用自定义中间件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM