【问题标题】:Passing Query Data Params in get HTTP request在获取 HTTP 请求中传递查询数据参数
【发布时间】:2019-09-11 21:46:35
【问题描述】:

所以,我的 Recipe 的后端生成 App 的 Get HTTP 端点接受参数并根据路由中传递的参数操作响应。

例如:如果我想接收仅属于某些美食组的食谱,我可以在我的 get HTTP 请求中传递这些美食组

"http://myrecipe.com/get/recipes/?cuisine=mediterranean&&cuisine=indian&&cuisine=chinese

这是我的 action.js

export const getRecipes = (params, access_token) => async dispatch => {
  const res = await axios.get(
          '/api/recipes'), 
          { params: {} }, 
          {headers: Authorization: `Bearer ${access_token}`}
     ); 
   dispatch({
       type: GET_RECIPES,
       payload: res.data
   }); 
}

和我的组件:

componentDidMount() {
   this.props.getRecipes({params: {cuisine: 'indian', cuisine: 'chinese', cuisine='mediterranean'} }, user.access_token); 
} 

但这似乎不起作用,我的代码有什么问题??

【问题讨论】:

  • 正如@user2343647 提到的,你在使用redux-thunk 吗?
  • 是的,我正在使用 redux-thunk

标签: reactjs redux axios redux-thunk reducers


【解决方案1】:

这行不通。异步调用使用 redux-thunk 或 redux-saga(我推荐后者)。

【讨论】:

    猜你喜欢
    • 2012-08-28
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2023-04-09
    • 2015-04-09
    • 2019-10-17
    相关资源
    最近更新 更多