简体   繁体   English

如何使用 react-query 从兄弟组件重新获取查询

[英]How to refetch queries from sibling component with react-query

I was wondering how to refetch a query from another sibling component with react-query.我想知道如何使用 react-query 从另一个同级组件重新获取查询。

Lets say I have Component A假设我有组件 A

 import {useQuery} from "react-query";

 function ComponentA(){

    const getData = async () => data //returns api data

    const {data, refetch} = useQuery(["queryA"], async () => await getData())

    return(

         <div>
           {data}
         </div>

   )}

And in Component B在组件 B 中

 import {useQuery, QueryClient} from "react-query";

 function ComponentB(){

    const queryClient = new QueryClient({
    defaultOptions: {
        queries: {
            staleTime: Infinity,
        },
    },
})

const refreshQueryFromComponentA = async () => {
     await queryClient.refetchQueries(["queryA"], { active: true })
}

    return(

         <div>
           <button onClick={refreshQueryFromComponentA}/>
         </div>

   )}

And in Page.js在 Page.js 中

 import ComponentA from "./componentA";
 import ComponentB from "./componentB";

 function Page(){

    return(

         <div>
           <ComponentA/>
           <ComponentB/>
         </div>

   )}

When I call the refreshQueryFromComponentA function in ComponentB I do not see the query refresh in ComponentA or a new call to the backend in the network tab.当我在 ComponentB 中调用 refreshQueryFromComponentA 函数时,我看不到 ComponentA 中的查询刷新或网络选项卡中对后端的新调用。 I also use the correct query-key but I am only able to refetch the query in ComponentA with the refetch() function which comes from the useQuery function.我也使用了正确的查询键,但我只能使用来自 useQuery 函数的 refetch() 函数重新获取 ComponentA 中的查询。

I think it's possible what I'm trying to do, since react-query uses context and should be available throughout the whole application.我认为我正在尝试做的事情是可能的,因为 react-query 使用上下文并且应该在整个应用程序中可用。 But maybe I'm using the wrong method or misinterpreted it.但也许我使用了错误的方法或误解了它。

Thanks in advance everyone!提前谢谢大家!

There needs to be one QueryClient at the top of your app.您的应用程序顶部需要有一个 QueryClient。 The QueryClient holds the queryCache, which stores your data. QueryClient 持有 queryCache,它存储您的数据。 If you create a new one in componentB, it won't share anything with componentA.如果您在 componentB 中创建一个新的,它不会与 componentA 共享任何内容。 Also, make sure to add it to the QueryClientProvider and retrieve it via useQueryClient() .此外,请确保将其添加到 QueryClientProvider 并通过useQueryClient()检索它。 The client also needs to be stable, so don't create a new one each render.客户端也需要稳定,所以不要每次渲染都创建一个新的。 This is from the first page of the docs:这是来自文档的第一页:

import { QueryClient, QueryClientProvider, useQuery } from 'react-query'
2 
3 const queryClient = new QueryClient()
4 
5 export default function App() {
6   return (
7     <QueryClientProvider client={queryClient}>
8       <Example />
9     </QueryClientProvider>
10   )
11 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM