TanStack Query in React Native - A Rick & Morty server-side filtering question (interactive example inside) #10136
Unanswered
spasmodiumspam-web
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I prepared an interactive/editable single file expo-snack example that you can run in the browser (or simply download the expo go app on your phone, scan the QR code and you're ready to go)
I have a Rick and Morty CRUD app with server-side filtering (multiple filters that can be applied at once) and infinite scrolling. We apply filters from a modal and those are status and gender. In the past I'd do this manually manually which was a nightmarish mess. Maintaining all those error, loading states and everything. But I recently discovered
useInfiniteQueryfrom TanStack Query and it seemed perfect. But while trying to implement it, I also discovered some pitfalls and I'm here asking you how to avoid them and what is good design in this scenario.In the code above (which has
console.logs, by the way, to make it easier to follow):log API {"page":1,"filters":{"name":"","status":"","gender":""}}log API {"page":1,"filters":{"name":"","status":"dead","gender":"male"}}log API UPDATE {"characterId":8,"updates":{"status":"alive","gender":"male"}}.Right now, the thing you'd normally do is invalidate the query and basically refetch the data with the existing filtering params, right?
But I don't wanna do that. Because this is a case in which I know exactly that the API will only change one thing - the status property. Not the order, not anything else. So I'd ideally want to spare a GET API call and only update the interface. In the past, without TanStack query, I'd just update the state, looking for the element with the ID I just clicked and modifying it or taking it out of the list or whatever. However, now I have to do something like this:
Needless to say, this is ugly as a sin, extremely verbose and I can't help myself but wonder...is this the wrong approach and I'm playing with fire? If we have even more complex filters, won't this become extremely tangled, hard to follow and write? Will new developers introduce bugs without ever knowing?
Questions:
I genuinely feel that with with all the staleTimes and options (some enabled by default) that TanStack Query has and me being a total beginner to the library, I'm just plotting for a disaster/subtle bugs to happen right now because, as I understand, every filtering combination is kept in a cache as a separate list and I have to know precisely what cache to update. And suddenly, this infinite scrolling hook which was supposed to simplify things is making the entire logic way more complicated that it initially seemed.
Am I totally doing it wrong?
Thank you in advance!
Beta Was this translation helpful? Give feedback.
All reactions