reactjsnext.jsreact-querystatic-site-generation

Is there a way to extract data from prefetch in order to pass as a parameter to another prefetch in 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.


Solution

  • 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)
    }