English 中文(简体)
在要求重新计算反应频率时,使用附带结果
原标题:Using cached results when calling refetch in react-query

I have a component that uses useQuery to fetch and display a list of Cities.

<>Cities.js

 const { data, isInitialLoading, isError, refetch } = useQuery(
    ["cities"],
    () => axios.get("http://localhost/cities"),
    {
      staleTime: 10000, 
    }
  );

  // Display loading indicator
  if (isInitialLoading) {
   return <div>Loading...</div>;
  }

  return (
      // Display cities
      {data.map(city => {
         return <p> City: {city.name} </p>
      })}

     // Call refetch method once clicked
     <button onClick={() => refetch()}> call refetch</button>
  ) 

上面显示的<><>但<><>>>>>>> > > > > > > > > 一旦被点击,即应为<>refetch数据。 我的问题是,当<代码>refetch时。 这种方法如上所示,可人工称为react-query查询cache,然后回到。 如果存在,或<代码>refetch>always向特定终点提出新的品牌要求? 我已经将<代码>staletime设定为10秒,因此,refetch将卡列结果退回10秒钟,然后在数据被考虑之后,再发出新的电话,以至<代码>? 如果没有,我可以采用哪些替代解决办法,以便在人工制作<代码>refetch时使用<代码>。 感谢。

问题回答

React Query的refetch方法确实在提出新的要求之前先检查切身。 如果海滩上的数据仍被视为新数据(即在<编码> 定期<<>>>/代码>内),则React Query将立即退还附在册的数据,然后引发背景图,以更新服务器的最新数据。 这样做是为了提供更平稳的用户经验,显示有线性的数据,同时确保快速更新数据,提供最新信息。

举例来说,在10秒钟的<代码>staletime上,电话refetch可在该时限内退还所附数据,并触发背景图,以更新各个海滩。 <代码>stale过期后,随后打电话到refetch时,仍可退还附卡的数据,同时启动新的电子要求,以更新背景的海滩。

If you want to ensure that manual refetches always make a new request without relying on the cache, you can use the force option when calling refetch. For example: JSX:- <button onClick={() => refetch({ force: true })}> call refetch</button>

<代码>:真实将绕过海滩,并总是向规定的终点提出新的要求。

或者,如果你想要在人工充电时更多地控制沥滤行为,那么你就可以根据您的申请要求,探索其他选择,如使用<编码>>可使用<<<<>>>>>>><<>条/代码>。

谢谢。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签