【发布时间】: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