Is there a way to extract data from prefetch in order to pass as a parameter to another prefetch in React-query

huangapple go评论75阅读模式
英文:

Is there a way to extract data from prefetch in order to pass as a parameter to another prefetch in React-query

问题

标题已经包含了基本信息。

我获取了一些数据,然后我需要一些额外的数据,但我需要一个ID值,这个ID值只能从第一次获取的数据中获取。整个页面都是SSG,我想知道是否有一种方法可以使用react-query来实现这个逻辑。

英文:

The title says it all, basically.

I fetch some data and then I want some additional data, but I need an ID value which I can only get from the first fetch's data. The whole page is SSG and I was wondering if there is a way to implement this logic with prefetch using react-query.

答案1

得分: 0

queryClient.prefetchQuery故意不返回任何数据,但是你可以使用queryClient.fetchQuery,它会返回数据。但请记住,你需要处理错误,因为fetchQuery不会这样做,而prefetch会。实际上,这是prefetchQuery的整个实现(为了简洁起见,省略了TS泛型):

prefetchQuery(options) {
  return this.fetchQuery(options).then(noop).catch(noop)
}
英文:

queryClient.prefetchQuery doesn't return any data on purpose, however, you can use queryClient.fetchQuery instead, which does return data. But keep in mind that you need to handle errors, because fetchQuery doesn't do that, while prefetch does. In fact, this is the whole implementation of prefetchQuery (leaving out the TS generics for brevity):

prefetchQuery(options) {
  return this.fetchQuery(options).then(noop).catch(noop)
}

huangapple
  • 本文由 发表于 2023年5月25日 02:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定