redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

Inconsistent behaviour of currentData between useQuery and useLazyQuery

Open arvind0598 opened this issue 7 months ago • 0 comments

Background: I'm working on simple page that essentially gets a list of filters (including a date range) from the user, sends an API request to the backend to GET data, and render it in a table. All dates in the date range are columns in the table. To ensure I correctly move to a loading state, and always render the correct date range, I switched to currentData and isFetching instead of data and isLoading.

Issue: My initial code was a useLazyQuery implementation. When I switched to currentData, it still returned the older data until the fetch was complete. After debugging for a bit, I switched to useQuery with a skip token, and it worked as expected.

Expected Behaviour: Ideally, I think useLazyQuery should behave the same as useQuery and send currentData as undefined instead of the older data. While the example in the documentation uses a useQuery, there's nothing saying that useLazyQuery should be expected to behave differently. We could add it to the docs if this is expected behaviour.

Relevant Documentation: From here.

While data is expected to be used in the majority of situations, currentData is also provided, which allows for a further level of granularity. For example, if you wanted to show data in the UI as translucent to represent a re-fetching state, you can use data in combination with isFetching to achieve this. However, if you also wish to only show data corresponding to the current arg, you can instead use currentData to achieve this.

Note: Using RTK ^2.2.6 and React ^18.3.3.

arvind0598 avatar Jul 03 '24 19:07 arvind0598