TRPC -useInfiniteQuery() refreshes all data when an input is varied, how to use?
So I have a TRPC infiniteQuery:
As an example, for each 3 gathered, I increase the value of page by 1, when
page = 2
, the page does a query, resets the pages content and triggers a repeating loop of queries.
I want to pass a varying value, be it string or number, to the query and be able to continue from the old content. Any help is welcome5 Replies
keepPreviousData: true,
Doesn't work either. Thought that might be it.seems to be how it's working out-of-the box
i.e. look at https://nextjs.trpc.io/
press "load more" a few times, click a list item and click back
Does that have a changing input by default?
The bit that is confusing me is that if I change the input.
So for example:
This works for example, however, I already have the pre-set values. If I send the
serverTags:
value in the query from the file (shown as tag:
) I get constant resets.
Ultimately, here is my question.
How do you correctly infiniteQuery when changing an input value?
Request 3 of tag: x
, then, a new query is triggered for request 3 of tag: y
, results of tag: x
get overwritten or replaced? At least when I use an array with an index selector number value.
I apologise if I'm coming across blunt or rude, it's not intended, I've been on this for a few days 😅
I'm just very confused why (what I imagine) a common need is giving me strange behaviourthis seems like a react query issue more than a trpc one - is it that you want to retain the state whenever you switch type?
could you ask over at the tanstack discord? i am not sure how to help with this
there might be some way of instructing infinite query to always show everything that is in the cache
That would be it yes, maybe storing the state would work?
Seems to defeat the point of
keepPreviousData: true
😅
After lots of ChatGPT work and a good break, I have a better understanding, but now I have more questions 😅
This behavior is happening because when you change the tag value in the query, it triggers a new query with the updated tag value. This causes the previous data to be replaced with the new data returned from the updated query.
Does TRPC have an option that works like this?
This will return an array of query results, one for each tag. You can then merge the results from all queries into a single array and display them as desired.This will return an array of query results, one for each tag. You can then merge the results from all queries into a single array and display them as desired.
If not, I believe my best approach is to trigger a normal query a stack an array client side with all the results. Same affect really.