仅当更改某些参数的值​时,如何查询React查询?
以下是我实际使用React查询的部分。
我的编码如下,反应查询不断恢复API。
只有在更改参数prodcode和Pagable的参数时才能进行API调用?
// react-query
const getReviewList = useQuery(
['getReviewList', prodCode, pageable],
() =>
ReviewApi.getReviewList({
prodCode,
pageable
}),
{
enabled: !!prodCode,
refetchOnWindowFocus: false,
onSuccess: (data) => {
if (!_.isUndefined(data)) {
const copiedReviewList = reviewList.slice();
copiedReviewList.push(...data.returnData);
// recoil state setting
setReviewList(copiedReviewList);
}
},
onError: () => {
setReviewList([] as Array<ReviewModel>);
}
}
);
The following is the part where I actually used React Query.
I coded as follows, and React Query keeps refetching the API.
How can I make the API call only when the parameters prodCode and pageable are changed?
// react-query
const getReviewList = useQuery(
['getReviewList', prodCode, pageable],
() =>
ReviewApi.getReviewList({
prodCode,
pageable
}),
{
enabled: !!prodCode,
refetchOnWindowFocus: false,
onSuccess: (data) => {
if (!_.isUndefined(data)) {
const copiedReviewList = reviewList.slice();
copiedReviewList.push(...data.returnData);
// recoil state setting
setReviewList(copiedReviewList);
}
},
onError: () => {
setReviewList([] as Array<ReviewModel>);
}
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
REACT查询不会进行倒架。您应该让React通过具有
PropCode
和Pagable
作为状态变量来照顾这一点。OnSuccess
和OnError
用于与数据相关的内容,因此请勿更新其中的React状态变量。我建议创建一个特定的查询类 -
usereViewListFetcher
,然后在组件中使用该类别。有点像这样:React Query will not refetch. You should let React take care for that by having
propCode
andpageable
as state variables.onSuccess
andonError
are meant to be used for data-related stuff, so don't update React state variables in there.I would suggest creating a specific query class -
useReviewListFetcher
and then use that one in your components. Somewhat like this: