React-query: Support setQueryData for new queries

Created on 16 Jan 2020  ยท  9Comments  ยท  Source: tannerlinsley/react-query

Hello,

I'm realizing that setQueryData only supports updating the cache for a query that has been used previously.

https://github.com/tannerlinsley/react-query/blob/db71fc0d874244da82490a05915d9324c527e9e7/src/index.js#L704-L706

What would you think of enabling it for new queries?

My use-case is that I create a resource in one page, get the resource back from the create mutation, and redirect to another page which fetches the resource. I would like to set the cache for that fetch since I have the data, but I currently am not able to.

Happy to help if feasible.

Thank you.

Most helpful comment

You can feed a prefetch any function, and the result will populate the cache. Nothing says you have to use the same function with the same query key ๐Ÿ˜‰ It could even be synchronous!
On Feb 1, 2020, 11:20 AM -0700, Steeve Lennmark notifications@github.com, wrote:

Does prefetchQuery not cover this use case?
I am staring at the code but I don't see how prefetchQuery lets me inject data without executing a fetch.
โ€”
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

All 9 comments

Does the prefetch method not cover this use case?

The difference here is I already have the data so I don't need to (re/pre)fetch it, it's closer to the updateQuery option of useMutation than it is to prefetch. But I can't use updateQuery since I don't know the variables before the mutation succeeds.

I need this for injecting data coming from the server on initial page load. After a quick dig through the code it looks like injecting data in a nonexisting query would require non-trivial refactoring. This could be solved by adding a new option to useQuery though, something that tells it not to fetch if initialData is provided.

Would that solve your problem too @tibotiber?

Does prefetchQuery not cover this use case?
On Feb 1, 2020, 11:13 AM -0700, Steeve Lennmark notifications@github.com, wrote:

I need this for injecting data coming from the server on initial page load. After a quick dig through the code it looks like injecting data in a nonexisting query would require non-trivial refactoring. This could be solved by adding a new option to useQuery though, something that tells it not to fetch if initialData is provided.
Would that solve your problem too @tibotiber?
โ€”
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

Does prefetchQuery not cover this use case?

I am staring at the code but I don't see how prefetchQuery lets me inject data without executing a fetch.

You can feed a prefetch any function, and the result will populate the cache. Nothing says you have to use the same function with the same query key ๐Ÿ˜‰ It could even be synchronous!
On Feb 1, 2020, 11:20 AM -0700, Steeve Lennmark notifications@github.com, wrote:

Does prefetchQuery not cover this use case?
I am staring at the code but I don't see how prefetchQuery lets me inject data without executing a fetch.
โ€”
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

/me run and hides

Yeah that totally solves it. Sorry for wasting your time! :-)

No waste! Happy to help ๐Ÿ˜
On Feb 1, 2020, 11:33 AM -0700, Steeve Lennmark notifications@github.com, wrote:

/me run and hides
Yeah that totally solves it. Sorry for wasting your time! :-)
โ€”
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

Haha gonna hide with @snorkypie, I really didnโ€™t think of this either and it definitely fits the use-case. Thanks all :)

Was this page helpful?
0 / 5 - 0 ratings