![](/img/trans.png)
[英]Approch for use rtk quey hook for sending request when ever params are exist not always?
[英]RTK useLazyQuery always making calls to the API even when called with same params on multiple components
我正在嘗試在執行惰性查詢時使用來自另一個組件的緩存數據來發出請求。 父組件(不是直接父組件,所以我不想通過 props 鑽取大型數據集)通過 RTK 進行惰性查詢,如下所示:
const [getDocuments, { data: contractDocuments, isLoading, isFetching }] = useLazyGetContractDocumentsQuery();
// inside a fetch function
getDocuments({
prop1,
prop2,
prop3,
});
然后子組件做同樣的事情
getDocuments({
prop1,
prop2,
prop3,
}).unwrap().then(documents=>{
console.log(documents);
});
我已經驗證了道具是相同的,返回的數據是相同的,但是在這兩種情況下,每次在任一組件中調用它時,它都會點擊 API 端點來獲取數據。 它永遠不會從緩存中獲取它。
文檔似乎說惰性查詢與常規 useQuery 不同,因為您選擇何時調用。 請參閱不同鈎子的功能比較。 但我無法與他們一起使用緩存。 我是否誤解了文檔,錯過了緩存規則的一些異常,或者有可能在另一個地方我以某種方式禁用緩存?
順便說一句,查詢如下所示:
getContractDocuments: builder.query<
ContractDocument[],
{
prop1: string;
prop2: string | null;
prop3: string;
}
>({
query: body => ({
url: '/GetDocuments',
body,
method: 'POST',
}),
transformResponse: (response: ContractDocument[]) => {
return response.map(d => ({
...d,
showIconViewFileInNewWindows: extensions.has(`${d.type}`),
}));
},
}),
默認情況下,惰性查詢觸發器總是發出請求調用,但是您可以提供第二個參數為 true 以在可用時使用緩存
getDocuments({
prop1,
prop2,
prop3,
}, true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.