【问题标题】:passing parameters in redux thunk在 redux thunk 中传递参数
【发布时间】:2022-08-12 23:05:23
【问题描述】:

我是对 redux 做出反应的新手,我需要在特定国家/地区的特定类别下获取产品 所以我的 api url 需要 2 个参数 categoryId 和 countryId 我做了一个 redux thunk 函数,只是面临一个问题,即如何将这 2 个参数传递给 api,我需要在更改 categoryId 时更改返回的产品

这是 Thunk 的代码:

export const productsFetch = createAsyncThunk(

\"产品/productsFetch\", 异步(数据)=> { 常量 { exportCountryId, categoryId } = 数据;

try {
  var myHeaders = new Headers();
  myHeaders.append(
    \"Authorization\",
    `bearer ${localStorage.getItem(\"token\")}`
  );
  var requestOptions = {
    method: \"GET\",
    headers: myHeaders,
    redirect: \"follow\",
  };
  const response = await fetch(
    `baseUrl/export-country/products?idc=${exportCountryId}&idcat=${categoryId}`,
    requestOptions
  );

  const data = await response.json();
  return data;
} catch (error) {
  console.log(error);
}

}


  • 如果您的参数发生变化,那么您需要每次都发出新请求并获取更新的数据。如果您不想担心存储和更新数据,那么您可以尝试 rtq 查询。

标签: reactjs react-redux redux-toolkit


【解决方案1】:

我的解决方案是将整个 thunk 动作包装在一个接受参数的函数中,当在功能组件中调度 thunk 动作时,我传递所需的参数

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多