TanStack Query in React Native - A Rick & Morty server-side filtering question (interactive example inside) #10136
-
|
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 In the code above (which has
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.
Replies: 1 comment
-
You’re still basically just doing that. The code gets verbose because you have to know that you have to take it out of the list instead of just updating a value. The thing you are doing that I think is “too much” is to iterate over all lists and update all of them. The only list that matters for the update is usually the one the user is currently on. Without TanStack Query, you only have the current data in state, so you don’t have the chance to manually update other lists, like ones with different filters. But you don’t have to do that. What I would do is:
queryClientInstance.invalidateQueries({ queryKey: ['characters'], type: 'inactive', refetchType: 'none' }) |
Beta Was this translation helpful? Give feedback.
You’re still basically just doing that. The code gets verbose because you have to know that you have to take it out of the list instead of just updating a value.
The thing you are doing that I think is “too much” is to iterate over all lists and update all of them. The only list that matters for the update is usually the one the user is currently on. Without TanStack Query, you only have the current data in state, so you don’t have the chance to manually update other lists, like ones with different filters.
But you …