简体   繁体   English

Apollo Client:使用修改后的变量重新获取数据,useQuery 还是 useLazyQuery?

[英]Apollo Client: refetch data with modified variables, useQuery or useLazyQuery?

I have a table showing data from server.我有一个表格显示来自服务器的数据。 The data is retrieved using Apollo Client query.使用 Apollo Client 查询检索数据。

The table has multiple filters and a search box.该表有多个过滤器和一个搜索框。 And all filters and text in the search are save in local storage.并且搜索中的所有过滤器和文本都保存在本地存储中。

The table and the fetch works like:表和提取的工作原理如下:

  1. read from localstorage , build variables for fetch (offset, limit, ...)localstorage读取,为获取构建变量(偏移量,限制,...)
  2. fetch with variables使用变量获取
  3. when filters or search change, refetch with modified variables当过滤器或搜索更改时,使用修改后的变量重新获取
  4. also save the modified variables to localstorage还将修改后的变量保存到localstorage

My question is: should I use useQuery or useLazyQuery for this purpose.我的问题是:为此我应该使用useQuery还是useLazyQuery

With useQuery , I may could do:使用useQuery ,我可以这样做:

// first fetch
const {data, loading, refetch} = useQuery(gql, { variables: {...filters from localstorage} })

// when filters changed
useEffect(() => {
  refetch({variable: {...changed filters}})
}, [filters])

For modified filters, is it possible to do refetch({variables: {...modified filters}}) ?对于修改后的过滤器,是否可以执行refetch({variables: {...modified filters}})

And with useLazyQuery , I may do:使用useLazyQuery ,我可以这样做:

const [getData, {data, loading, refetch}] = useLazyQuery(gql)

// first fetch
useEffect(() => {
  getData({variables: {...filters from localstorage}})
}, [])

// when filters changed
useEffect(() => {
  refetch({variable: {...changed filters}}) // ?
  // or
  getData({variables: {...modified filters}}) // ?
}, [filters])

refetch or getData , which should I use? refetchgetData ,我应该使用哪个?

I think you should just use useQuery , but instead of keeping your filters in localeStorage, keep it also in a locale React state.我认为你应该只使用useQuery ,但不要将过滤器保存在 localeStorage 中,而是将它也保持在 locale React 状态。 So when you update the filters state, useQuery will know and refetch the query with new parameters.因此,当您更新过滤器状态时,useQuery 将知道并使用新参数重新获取查询。 And you don't need separate useEffect to trigger the refetch.而且您不需要单独的 useEffect 来触发重新获取。

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

相关问题 Apollo-client 有条件地同时运行 useLazyQuery 和 useQuery - 最好的方法是什么? - Apollo-client run both useLazyQuery and useQuery conditionally - what's the best approach? @apollo/client 反应钩子 useQuery() 数据未定义 - @apollo/client react hook useQuery() data undefined 错误:Apollo useLazyQuery、useMutation、useQuery 中的无效挂钩调用 - Error: Invalid hook call in Apollo useLazyQuery , useMutation, useQuery apollo 的 useQuery 数据在 client.resetStore() 后不更新 - apollo's useQuery data does not update after client.resetStore() Lodash Debounce 和 Apollo Client useLazyQuery debounce 一次 - Lodash Debounce and Apollo Client useLazyQuery debounces once Apollo Client - 当变量发生变化时,可以只重新获取较大查询的片段吗? - Apollo Client - Possible to refetch only a fragment of a larger query when it's variables change? 尝试重新获取时 Apollo 客户端上的循环错误 - Circular error on Apollo Client when trying to refetch Apollo Client - useQuery - 如何将参数设置为可选 - Apollo Client - useQuery - how to set parameter as optional 尝试使用 Apollo 查询重新获取数据 - Trying to refetch the data using Apollo query 只有在浏览器中刷新页面时,数据才在 apollo 客户端的 usequery 中变为 undefined - Data becomes undefined in usequery of apollo client only when refreshing the page in the browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM